我在这种情况下:
app group
共享目录中的CoreData的SQLite文件插入新条目。我使用了CoreDataStack.shared.mainContext.reset()
,但是它不起作用。
我的核心堆栈核心(我的主应用程序和应用程序扩展名均使用此代码):
import CoreData
public class CoreDataStack {
let appID = "xxx"
let modelFileName = "xxx"
let defaultAppGroupID = "xxx"
public static let shared = CoreDataManager()
public var mainContext: NSManagedObjectContext {
return mainPersistentContainer.viewContext
}
// MARK: - Core Data stack
lazy var managedObjectModel: NSManagedObjectModel = {
let bundle = Bundle(for: type(of: self))
let momdURL = bundle.url(forResource: modelFileName, withExtension: "momd")!
return NSManagedObjectModel(contentsOf: momdURL)!
}()
lazy var mainPersistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: modelFileName, managedObjectModel: managedObjectModel)
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: defaultAppGroupID)!.appendingPathComponent("\(modelFileName).sqlite"))]
container.loadPersistentStores(completionHandler: { (_, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
public func saveMainContext() {
let context = mainPersistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
fileprivate func applicationDocumentsDirectory() -> URL {
let fileManager = FileManager.default
if let url = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.io.wildcat.InfoFlow") {
return url
} else {
let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
return urls[0]
}
}
}
答案 0 :(得分:1)
我提交了代码级支持,这是答案。
您可以考虑两种解决方法:
context.reset()
// MARK: Reload the data
// NSFetchedResultsController.performFetch()
// UITableView.reloadData()