環境: iOS SDK9
在你想比對NSNumber時,從JSON轉出來的數值出現了NSTaggedPointerString,你會比對不到,於是得到nil.
- (Visitor *)visitorByID:(NSNumber *)uid {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Visitor" inManagedObjectContext:self.managedObjectContext]];
if ([uid isKindOfClass:[NSNumber class]]) {
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"self.visitor_id == %@", uid]];
} else {
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSNumber *numUid = [numberFormatter numberFromString:(NSString *)uid];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"self.visitor_id == %@", numUid]];
}
NSError * error = nil;
NSArray * result = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (error!=nil) {
SOLogError(@"%s[%d:%s]: %@", __FILE__, __LINE__, __FUNCTION__,error.localizedDescription);
}
}
解法:
把NSTaggedPointerString再轉回NSNumber一次
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSNumber *numUid = [numberFormatter numberFromString:(NSString *)uid];
2018年7月1日 星期日
CoreData的performBlock和performBlockAndWait的差異
performBlock
會異步執行在context的thread之中(不會阻塞當前的thread)
performBlockAndWait
會同步執行在context的thread之中(會阻塞當前的thread,直到執行完block中的code再返回)
NSManagedObjectContext save()
https://developer.apple.com/documentation/coredata/nsmanagedobjectcontext/1506866-save
https://developer.apple.com/documentation/coredata/nsmanagedobjectcontext/1506954-haschanges
https://developer.apple.com/documentation/coredata/nsmanagedobjectcontext/1506954-haschanges
訂閱:
文章 (Atom)