首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将核心数据堆栈更新为applicationWillEnterForeground

将核心数据堆栈更新为applicationWillEnterForeground
EN

Stack Overflow用户
提问于 2015-04-16 09:43:38
回答 1查看 211关注 0票数 0

我和App groups在两个应用程序之间共享了相同的App groups

当我在应用程序A中添加录音并打开App (第一次启动)时,App将正确检索数据。

当我在应用程序A和应用程序B中添加记录时(已经在后台启动),我希望同步数据,当应用程序B返回到前台时,它可以检索数据。

这就是为什么当应用程序B回到前台时,我将核心数据袋更新为applicationWillEnterForeground。哪条路是对的?

代码语言:javascript
复制
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试过了

代码语言:javascript
复制
   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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-16 16:53:45

如果应用程序已经处于活动状态,则不应该再次设置持久存储协调器。Xcode模板中使用的标准延迟初始化是足够和更好的。

相反,您可能需要侦听NSManagedObjectContextObjectsDidChangeNotification并酌情更新您的UI。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29670955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档