i handle 2 complete core data stacks in app:
- the 1 provided default in
appdelegate. - a second stack create in order perform
nsmanagedobjectupdates 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
maincontextintended shownsmanagedobjectinformation throughout app. - the
privatecontextintended used call web services updated data, create newnsmanagedobjectreceived information, , compare new objects ones app have.
my questions regarding are:
- should
privatecontextused callingperformblockorperformblockandwait? include related operations, such s reading/inserting objectsprivatecontext, , clearing/saving it? - the
maincontextsupposed associated main queue/thread, right? related operations should performed in main thread... - having account
privatecontexthas own full core data stack... if save objects, stored @ same.sqlitefile ones when savingmaincontext? or such file way "duplicated"? - if
privatecontextshould save data private queue, ,maincontextshould used in main thread, cause problem fetchmaincontextdata savedprivatecontext?
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
Post a Comment