ios - How to implement Cora Data Persistent Management -


i have following structure far:
- singleton networkingmanager (login, logout api calls)
- subclass nsmanagedobject (with son extension)

i don't know how structure part of app?
need persistentmanager/global objectmanagedcontext?

here classes:

networkingmanager (api-call)

func getcontacts() {           get(apiurl.url_contacts ,parameters: nil,         { (operation : nsurlsessiondatatask!, response : anyobject!) -> void in             var contacts = [contacts]()             contacts <<<<* response             //_sharedpersistentmanager.save(contacts!)         }, { (operation : nsurlsessiondatatask!, error : nserror!) -> void in            println("error contacts")     }) } 

model

import foundation import coredata  class contacts: nsmanagedobject, deserializable {  @nsmanaged var firstname: string @nsmanaged var lastname: string @nsmanaged var id: string  required init(data: [string: anyobject]) {     let managedcontext = (uiapplication.sharedapplication().delegate         appdelegate).managedobjectcontext!     let entity = nsentitydescription.entityforname("contacts", inmanagedobjectcontext: managedcontext)!     super.init(entity: entity, insertintomanagedobjectcontext: managedcontext)      self.id <<< data["id"]     self.firstname <<< data["1"]     self.lastname <<< data["2"] } } 

this code fails... (bad_access)

i struggle basic class design. kind of post pseudo-code , can understand basic concept.

all tutorials explain how save or fetch data, not real concept whole app...

i'm looking basic concept like:
- should implement 1 persistent manager singleton class

//persistentmanager
- global objectmanagedcontext
- singleton
- func save (model1: model1)
- func save (model2: model2)
- ....

//networkingmanager
- singleton
- ...

edit:
- added code
- added further explanation

typically app have 1 database (but may have more one, depending on requirements). if assume need 1 need create core data database (persistent store) , @ minimum 1 nspersistentstorecoordinator , 1 managedobjectcontext in order create , update database records via managedobjects. use template app sample code doing this.

i not try using managedobjects init() method creating object, instead use helper function in class this. init() method called automatically when create object might use set default values properties.

below example managedobject (swift class) , objective-c helper function. apologies in advance, need convert equivalent swift code.

// //  documentcategory.swift //  sample //  import foundation import coredata  @objc(documentcategoryx) class documentcategoryx: nsmanagedobject {      @nsmanaged var image: nsdata     @nsmanaged var name: string     @nsmanaged var sortindex: nsnumber     @nsmanaged var viewname: string     @nsmanaged var documents: nsset  } 

sample helper function create managed object (a core data database record)

- (nsmanagedobject*)createnewcategory:(nsstring*)entityname sortindex:(int)sortindex withname:(nsstring *)name viewname:(nsstring *)viewname icon:(uiimage*)icon; {     nsmanagedobject *newcat = [nsentitydescription                                insertnewobjectforentityforname:entityname                                inmanagedobjectcontext:[self managedobjectcontext]];     [newcat setvalue:[nsnumber numberwithint:sortindex] forkey:@"sortindex"];     [newcat setvalue:name forkey:@"name"];     [newcat setvalue:viewname forkey:@"viewname"];      [newcat setvalue:uiimagepngrepresentation(icon) forkey:@"image"];  // ios     return newcat; } 

swift function save moc. call whenever need make sure things written disk.

// mark: - core data saving support      func savecontext () {         if let moc = self.managedobjectcontext {             var error: nserror? = nil             if moc.haschanges && !moc.save(&error) {                  //flog("unresolved error \(error), \(error!.userinfo)")                 // need message user warning save failed!             }         }     } 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -