我在应用程序中使用RestKit并配置了映射器并且工作正常。我已经决定使用CoreData进行缓存,而通常的fecthController用于在表视图中显示核心数据模型。我的应用程序的屏幕是一个带有三个选项卡的UITabViewController。在每个选项卡中,我有来自不同实体的不同信息,我可以在选项卡之间切换,所有工作正常。问题是当我在选项卡中进行搜索时,我丢失了其他选项卡中的数据。我深入研究了这个问题,并且我已经看到了以下内容:当我搜索某些东西时,正在删除我的其他实体!!我试图创建不同的上下文和一些与核心数据相关的内容,但没有任何作用。
// Configuration stuff
let objectManager: RKObjectManager = RKObjectManager(baseURL: baseURL);
let managedObjectModel: NSManagedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL!)!;
let managedObjectStore: RKManagedObjectStore = RKManagedObjectStore(managedObjectModel: managedObjectModel);
objectManager.managedObjectStore = managedObjectStore;
managedObjectStore.createPersistentStoreCoordinator();
do {
try managedObjectStore.addSQLitePersistentStoreAtPath(storePath, fromSeedDatabaseAtPath: seedPath, withConfiguration: nil, options: nil);
} catch let err as NSError {
fatalError()
}
// Create the managed object contexts
managedObjectStore.createManagedObjectContexts();
managedObjectStore.managedObjectCache = RKInMemoryManagedObjectCache(managedObjectContext: managedObjectStore.persistentStoreManagedObjectContext)
// Mapping
let entityMapping: RKEntityMapping = RKEntityMapping(
forEntityForName: "Entity",
inManagedObjectStore: RKObjectManager.sharedManager().managedObjectStore
);
entityMapping.identificationAttributes = ["id"];
entityMapping.addAttributeMappingsFromArray([
"id", "firstName", "lastName", "birthdate"
]);
//Get Data from server
class Datalist{
class fun fetchData(successClosure:[Entity]->Void,failure:String->Void){
RKObjectManager.sharedManager().getObjectsAtPath(
path,
parameters: nil,
success: {
( operation: RKObjectRequestOperation!, result: RKMappingResult!) -> Void in
dispatch_async(dispatch_get_main_queue(), {
() -> Void in
successClosure(result.array());
});
},
failure: {
( operation: RKObjectRequestOperation!, error: NSError!) -> Void in
dispatch_async(dispatch_get_main_queue(), {
() -> Void in
failureClosure();
});
}
);
}
}
}
//Show data
class ListViewController:NSFetchedResultsControllerDelegate{
private var fetchedResultsController: NSFetchedResultsController!;
override func viewDidLoad() {
super.viewDidLoad();
fetchedResultsController = NSFetchedResultsController(
fetchRequest: fetchRequest, managedObjectContext: RKManagedObjectStore.defaultStore().mainQueueManagedObjectContext,
sectionNameKeyPath: "formatted",
cacheName: nil)
fetchedResultsController?.delegate = self
getData()
}
private func getData(){
DataList.fetchData({data in
let ids = getEntitiesId(data)
fetchedResultsController.fetchRequest.predicate = createPredicateFromIds(ids)
try! self.fetchedResultsController?.performFetch()
},failure:{message in
showMessage(message)
})
}
func createPredicateFromIds( entityIds: [Int] ) -> NSPredicate {
let predicate: NSPredicate = NSPredicate( format: "id IN $ENTITYIDS" ).predicateWithSubstitutionVariables(["ENTITYIDS" : entityIds]);
return predicate;
}
}
答案 0 :(得分:1)
最后,我发现了有关删除孤立对象的问题。因为在三个选项卡中是不同的实体,当我在实体中执行搜索时,使其他实体成为孤立并删除数据!仅覆盖此属性:
override var deletesOrphanedObjects: Bool {
get{
return false;
}
set{
}
};