项目未从Share Extension

时间:2016-11-25 12:14:11

标签: ios objective-c swift core-data ios8-share-extension

我确实创建了分享扩展程序。使用此扩展我试图将新项目插入CoreData。代码如下。创建变量 context document context.save()没有失败。使用main / host应用程序插入CoreData可以正常工作并呈现给UI。

我注意到另一件事是来自Share Extension的所有插入都存储在其他地方,但不在主应用程序中。看起来这些项目未合并到主CoreData。

为什么我的项目插入CoreData抛出共享扩展未合并到主CoreData?

let context = CoreDataStackManager.sharedManager.persistentContainer.viewContext
let document = SGDocument(context: context)
document.name = "Document Name"
do {
    try context.save()
} catch {
    fatalError("Unresolved error \(error)")
}

PS:Here是类似的问题,但没有答案。

1 个答案:

答案 0 :(得分:0)

我找到了它的解决方案。

// In the extension I am setting that VC needs to be updated/refreshed.
UserDefaults.group.set(true, forKey: .udRefreshDocumentsVC)

// In the view controller adding listener for application will enter foreground.
NotificationCenter.default.addObserver(forName: .UIApplicationWillEnterForeground, object: nil, queue: nil, using: applicationWillEnterForeground)

// In the applicationWillEnterForeground method do reload.
    func applicationWillEnterForeground(notification: Notification) {
        if UserDefaults.group.bool(forKey: .udRefreshDocumentsVC) {
            _fetchedResultsController = nil
            tableView.reloadData()
            UserDefaults.group.set(false, forKey: .udRefreshDocumentsVC)
        }
    }