在應用自動升級為新的ARC,從CoreData中拿數據時出現錯誤,下面是方法代碼:
NSArray *userinfo =[self checkData:self.username];
//This return fault data in array
-(NSArray*) checkData:(NSString*)loginUsername{
id appDelegate = (id)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:[[appDelegate managedObjectContext] persistentStoreCoordinator]];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *userEntity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:managedObjectContext];
[request setEntity:userEntity];
[request setPredicate:[NSPredicate predicateWithFormat:@"userName == %@",loginUsername ]];
NSError *error;
NSArray *userInfo =[managedObjectContext executeFetchRequest:request error:&error];
//NSLog(@"Number of account%@",[userInfo valueForKey:@"userName"]);
if ([userInfo valueForKey:@"userName"] != nil && userInfo.count != 0)
return userInfo;
else return nil;
}
出錯日志:
<User: 0x12b73db0> (entity: User; id: 0x12b6dc20 <x-coredata://7388F0B7-E583-4BF0-BE64-08AD014EC583/User/p2> ; data: <fault>)
2012-12-23 10:09:18.802 test[78839:c07] -[NSNull isEqualToString:]: unrecognized selector sent to instance 0x28ac678
2012-12-23 10:09:18.803 test[78839:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull isEqualToString:]: unrecognized selector sent to instance 0x28ac678'
*** First throw call stack:
(0x277b012 0x2018e7e 0x28064bd 0x276abbc 0x276a94e 0xb4028 0x3e3be 0x1812e59 0x1810f22 0x181216a 0x1810edd 0x1811055 0x175e338 0x5d3a81 0x5d2d33 0x610e3a 0x271d8fd 0x6114bc 0x611435 0x4fb3a0 0x26fef3f 0x26fea39 0x2721734 0x2720f44 0x2720e1b 0x31027e3 0x3102668 0xc5465c 0x2806 0x2735 0x1)
libc++abi.dylib: terminate called throwing an exception
換成用AppDelegate的managedObjectContext:
id appDelegate = (id)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:[[appDelegate managedObjectContext] persistentStoreCoordinator]];
把這些代碼換成:
NSManagedObjectContext *managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];