ios - How should I correctly manage a full Core Data stack in private queue? -


i handle 2 complete core data stacks in app:

  • the 1 provided default in appdelegate.
  • a second stack create in order perform nsmanagedobject updates in private queue, avoid blocking ui.

i have class create second "auxiliary" core data stack, , way:

class coredatastack: nsobject {     class func getprivatecontext() -> nsmanagedobjectcontext {         let bundle = nsbundle.mainbundle()        let modelurl = bundle.urlforresource("myapp", withextension: "momd")        let model = nsmanagedobjectmodel(contentsofurl: modelurl!)!         let psc = nspersistentstorecoordinator(managedobjectmodel: model)         let privatecontext = nsmanagedobjectcontext(concurrencytype: nsmanagedobjectcontextconcurrencytype.privatequeueconcurrencytype)        privatecontext.persistentstorecoordinator = psc         let documentsurl = coredatastack.applicationdocumentsdirectory()        let storeurl = documentsurl.urlbyappendingpathcomponent("myapp.sqlite")         let options = [nsmigratepersistentstoresautomaticallyoption: true]         var error: nserror? = nil        let store: nspersistentstore?        {            store = try psc.addpersistentstorewithtype(nssqlitestoretype, configuration: nil, url: storeurl, options: options)        } catch let error1 nserror {            error = error1            store = nil        }         if store == nil {            print("error adding persistent store: \(error)")            abort()        }         return privatecontext    }     class func applicationdocumentsdirectory() -> nsurl {        let filemanager = nsfilemanager.defaultmanager()        let urls = filemanager.urlsfordirectory(.documentdirectory, indomains: .userdomainmask)         return urls[0]    }  } 

i need firstly confirm/clarify points:

a) legal/correct create full core data stack use context in private queue way i'm doing?

b) creating new nsmanagedobjectmodel same resource , same .sqlite file 1 used in appdelegate default core data stack cause problems?

about managing both contexts have (the default in appdelegate, let's call maincontext, , 1 create in private queue, let's call privatecontext):

  • the maincontext intended show nsmanagedobject information throughout app.
  • the privatecontext intended used call web services updated data, create new nsmanagedobject received information, , compare new objects ones app have.

my questions regarding are:

  1. should privatecontext used calling performblock or performblockandwait? include related operations, such s reading/inserting objects privatecontext, , clearing/saving it?
  2. the maincontext supposed associated main queue/thread, right? related operations should performed in main thread...
  3. having account privatecontext has own full core data stack... if save objects, stored @ same .sqlite file ones when saving maincontext? or such file way "duplicated"?
  4. if privatecontext should save data private queue, , maincontext should used in main thread, cause problem fetch maincontext data saved privatecontext?

i need understand , correctly manage core data concurrency in app, i'm making mess persistence staff , i'm finding errors in operations seemed work... in advance.


Comments