Buc*_*cky 1 cocoa core-data nsfetchrequest xmppframework automatic-ref-counting
我有一个方法执行NSFetchRequest以获取托管对象数组(特别是XMPPUserCoreDataStorageObjects).在performUserFetch返回数组之前对象是正确的,我可以打印所有的displayNames,但是一旦我将数组返回到printUserInfo,对象就会进入故障状态,除了Core Data不会带来问题他们回来了!
- (NSArray*)performUserFetch
{
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:[xmppRosterStorage persistentStoreCoordinator]];
[context setUndoManager:nil];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject" inManagedObjectContext:context];
NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSError *err;
NSArray *result = [context executeFetchRequest:fetchRequest error:&err];
return result;
}
- (void)printUserInfo
{
NSArray *result = [self performUserFetch];
for(XMPPUserCoreDataStorageObject *user in result)
{
NSString *dn = user.displayName;
NSLog(@"Display name = %@", dn);
}
NSLog(@"%@",result);
}
Run Code Online (Sandbox Code Playgroud)
我每隔5秒调用一次printUserInfo,并且在performUserFetch中调用结果数组,但是printUserInfo中的所有元素都被置于pot中,并且数组中的所有元素都已从内存中清除.那没关系,但是当我调用user.displayName时,故障没有解决,所以dn的值为null,用户的描述是
"<XMPPUserCoreDataStorageObject: 0x10219fd60> (entity: XMPPUserCoreDataStorageObject; id: 0x1021a3390 <x-coredata://324B9E93-BAD1-42B4-B7DB-2A62CA69BA13/XMPPUserCoreDataStorageObject/p127> ; data: <fault>)"
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?!
(信息:10.7 SDK,每隔5秒调用printUserInfo,并在ARC下运行)
只是猜测.尝试以下内容将是有希望的
另一种解决方案BTW是使用NSFetchedResultsController.在故障方面,它的设计特别可靠和高效.