我找到了清理内存存储上下文的解决方法。此解决方法是从内存对象图中清理一些内存,而不是所有内容。
if inMemoryManagedObjectContext != nil{
if let stores = inMemoryManagedObjectContext.persistentStoreCoordinator?.persistentStores{
for store in stores{
do{
try inMemoryManagedObjectContext.persistentStoreCoordinator?.remove(store)
}catch{
print("Cleanup InMemoryManagedObjectContext error;\(error)")
}
}
}
inMemoryManagedObjectContext = nil
}
谢谢 Jon Rose:如果我必须删除所有实体,则会出现性能问题,因为我有更多实体。另外我不确定删除实体会立即清理内存。
谢谢 Sandeep Bhandari:PersistentContainer 适用于 iOS 10 及更高版本,我的应用程序应该支持 iOS 8。
上下文重置也不会对内存清理产生任何影响。如果有任何其他方法可以清理内存中的上下文/persistentStoreCoordinator,请告诉我。
谢谢!