问题描述
我对核心数据
感到烦恼。我的应用程序需要从iPhone获取联系人并将其保存在我的数据库中。我正在尝试在后台主题中这样做。
I've an annoying problem with Core Data
. My app need to get contacts from iPhone and save it in my database. I'm trying to do that in background thread.
我使用上面的代码:
[self performSelectorInBackground:@selector(fetchingContact) withObject:nil];
-(void)fetchingContact{
// Some Code
for (int i = 0; i < nPeople; i++)
{
//Some Code
NSManagedObjectContext *context = [APP_DELEGATE managedObjectContext];
ABRecordID recordID = ABRecordGetRecordID(person);
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(contactId = '%d')",recordID]];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
//crash
NSArray *contactObjArray = [context executeFetchRequest:fetchRequest error:&error];
//crash
if (error) {}
Contact *contacts;
if (contactObjArray.count == 0) {
contacts = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:context];
}else {
contacts = [contactObjArray objectAtIndex:0];
}
//Some Code
}
}
在AppDelegate中:
- (NSManagedObjectContext *)managedObjectContext
{
NSLog(@"managedObjectContext");
// Returns ;the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc]initWithConcurrencyType: NSMainQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;
}
这里我尝试保存在我的核心数据
但它崩溃并出现错误:
Here I try to save in my Core Data
but it crash with error :
NSArray *contactObjArray = [context executeFetchRequest:fetchRequest error:&error];
我已经在线搜索,我找到了很多东西,但没有任何帮助我。当我运行它时,没有地方核心数据
被更改,或者联系实体。这样做非常奇怪。
I search already online, I found a lot of things but nothing helps me. When I run that, there is no place where Core Data
is changed, or Contact entity. That does this error very strange.
如果我在主线程中运行它我没有错误/没有崩溃,但如果应用程序在那段时间退出(执行时) )我丢失了所有内容联系
If I run it in main thread I get no errors/ no crash, but if the app is quit in that time(while is executed) I lose all content from Contact
请随时提供帮助。如果需要更多信息,请告诉我。
Please, any help. Tell me if needs more informations.
推荐答案
当您尝试获取核心数据时,会发生此错误。
This error happen when you are modifying core data while you try to get them.
这也可能是你正在做的循环的原因,你在coredata中插入一个新对象而不保存,然后再进行其他检索。尝试保存您的managedobjectcontext:
That also could be cause of the loop you're doing, you are inserting a new object in coredata without saving before you do an other retrieve. Try saving your managedobjectcontext :
收藏
我对Core Data有一个恼人的问题。我的应用程序需要从iPhone获取联系人并将其保存在我的数据库中。我试图在后台线程中这样做。
favoriteI have an annoying problem with Core Data. My app need to get contacts from iPhone and save it in my database. I'am trying to do that in background thread.
我使用上面的代码:
[self performSelectorInBackground:@selector(fetchingContact) withObject:nil];
-(void)fetchingContact{
// Some Code
for (int i = 0; i < nPeople; i++)
{
//Some Code
NSManagedObjectContext *context = [APP_DELEGATE managedObjectContext];
ABRecordID recordID = ABRecordGetRecordID(person);
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(contactId = '%d')",recordID]];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *contactObjArray = [context executeFetchRequest:fetchRequest error:&error];
if (error) {}
Contact *contacts;
if (contactObjArray.count == 0) {
contacts = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:context];
[context save:&error];
}else {
contacts = [contactObjArray objectAtIndex:0];
}
//Some Code
}
}
如果这不能解决您的问题,可以检查您的方法是否被多次调用。
If that doesn't solve your problem, maybe check if your method is called multiple times.
这篇关于核心数据在枚举时发生了变异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!