我是Core Data的新手,想知道是否可以根据对象的属性获取对象,更具体地说,是我分配给它的uniqueID。我之所以尝试这样做,是因为我正在与一个web服务器连接,该服务器提供将更新Core Data的数据。我希望搜索每个web服务器对象,检查时间戳,如果时间戳不同,则从核心数据中检索该对象,然后进行更新。我已经考虑过使用existingObjectWithId,但似乎我必须知道要搜索的对象,或者该对象的ID。我还考虑过对两个数组中的数据进行排序,然后同时检查每个数组,但我认为这不可行。
到目前为止,我正在做的是:
-(NSMutableArray*) updateRootBeers:(NSMutableArray*)webRootBeerList{
NSManagedObjectContext* moc = [self managedObjectContext];
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Rootbeer"];
coreDataRootBeerList = [[moc executeFetchRequest:fetchRequest error:nil]mutableCopy];
//check to see if core data has data, if not, call function to populate the list
if (coreDataRootBeerList.count <=0) {
[self addRootBeerToCoreData:webRootBeerList];
}else{
//otherwise I want to update the core data object if object data is different.
for(RootBeer* currentRootBeer in webRootBeerList){
RootBeer* mRootBeer = [moc existingObjectWithId:currentRootBeer error:nil];
}
}
}我还考虑过使用嵌套的for循环来检查每个数组中的数据,但这似乎是糟糕的编码。任何帮助或想法都是很棒的。
发布于 2013-05-20 07:10:57
你想做一个NSFetchRequest。您可以设置实体并提供谓词。非常简单和干净。
https://stackoverflow.com/questions/16640394
复制相似问题