我和App groups在两个应用程序之间共享了相同的App groups。
当我在应用程序A中添加录音并打开App (第一次启动)时,App将正确检索数据。
当我在应用程序A和应用程序B中添加记录时(已经在后台启动),我希望同步数据,当应用程序B返回到前台时,它可以检索数据。
这就是为什么当应用程序B回到前台时,我将核心数据袋更新为applicationWillEnterForeground。哪条路是对的?
let directory = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.com.sl.sharedData");
let url = directory?.URLByAppendingPathComponent("sharedData.sqlite")
let store = self.persistentStoreCoordinator?.persistentStoreForURL(url!)
var error : NSError?
if false == self.persistentStoreCoordinator?.removePersistentStore(store!, error: &error)
{
println("error = \(error!.localizedDescription)")
return
}
let options = [
NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true,
// Adding the journalling mode recommended by apple
NSSQLitePragmasOption: ["journal_mode": "DELETE"]
]
var failureReason = "There was an error creating or loading the application's saved data."
if self.persistentStoreCoordinator?.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: options, error: &error) == nil {
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
_persistentStoreCoordinator = nil
_managedObjectContext = nil
let rootViewController = self.window!.rootViewController as ViewController
rootViewController.context = self.managedObjectContext不幸的是,它不像我想的那样起作用了。每次输入applicationWillEnterForeground时都会对检索到的数据进行配音。哪条路是对的?
//编辑2014/04/17 :尝试使用芒迪的解决方案
我和NSManagedObjectContextObjectsDidChangeNotification试过了
func applicationWillEnterForeground(application: UIApplication) {
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "mergeContextChangesForNotification:",
name: NSManagedObjectContextObjectsDidChangeNotification,
object: managedObjectContext)
let rootViewController = self.window!.rootViewController as ViewController
rootViewController.context = managedObjectContext
}
}
func mergeContextChangesForNotification(notification : NSNotification){
let otherContext: NSManagedObjectContext = notification.object as NSManagedObjectContext
if((otherContext != managedObjectContext) && (otherContext.persistentStoreCoordinator == managedObjectContext.persistentStoreCoordinator)){
managedObjectContext.performBlockAndWait{ () -> Void in
self.managedObjectContext.mergeChangesFromContextDidSaveNotification(notification)
}
}
}mergeContextChangesForNotification已被调用,但我从未在以下条件下输入:if otherContext != managedObjectContext) && (otherContext.persistentStoreCoordinator == managedObjectContext.persistentStoreCoordinator
发布于 2015-04-16 16:53:45
如果应用程序已经处于活动状态,则不应该再次设置持久存储协调器。Xcode模板中使用的标准延迟初始化是足够和更好的。
相反,您可能需要侦听NSManagedObjectContextObjectsDidChangeNotification并酌情更新您的UI。
https://stackoverflow.com/questions/29670955
复制相似问题