所以我有一种方法可以下载“皮肤”我的应用程序的图像的网址。它已经在 iOS 5 中运行了几个月,我在我的 xcdatamodel 中的实体中添加了一个字符串类型的新属性,但它停止了工作。即使我删除了该属性,它仍然不起作用。
当我清理 xcode 并从 iPad 中删除应用程序时,然后启动该应用程序。
在 iOS 5 中,我收到此错误:“线程 1:EXC_BAD_ACCESS(code=1,address=0x1)”,如果我按照堆栈跟踪,这发生在我调用setValue:forKey:
.
但是,如果我在 iOS 6 中清理并运行,则效果很好。
- (void)fetchSkinWithCompletion:(void (^)(void))completionBlock failure:(void (^)(void))failureBlock {
NSString *skin = [self skinName];
if (skin) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/skin/%@", BASE_URL, skin]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
NSError *jsonError = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&jsonError];
if (!jsonError) {
Skin *s = (Skin *)[Skin findFirstObjectByAttribute:@"name" withValue:skin inContext:[AppDelegate moc]];
if (s == nil) {
s = [NSEntityDescription insertNewObjectForEntityForName:@"Skin" inManagedObjectContext:[AppDelegate moc]];
}
for (NSString * key in json) {
/******** THIS IS WHATS BLOWING UP IN iOS 5 *************/
[s setValue:[json objectForKey:key] forKey:key];
}
[[AppDelegate moc] save:nil];
self.selectedSkin = s;
[self downloadImageAssetsForSkin:self.selectedSkin completionBlock:completionBlock];
return;
} else {
DLog(@"Error parsing JSON:%@", jsonError);
}
} else {
DLog(@"User with Device ID:%@ failed fetch skin with error:%@.", [AppDelegate uniqueID], error);
}
}
failureBlock();
return;
}
findFirstObjectByAttribute:withValue:inContext
是对 NSManaged Object 的补充。
+ (NSManagedObject *)findFirstObjectByAttribute:(NSString *)attr_name withValue:(id)value inContext:(NSManagedObjectContext *)context {
NSFetchRequest *fetch = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([self class])];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", attr_name, value];
fetch.predicate = predicate;
fetch.fetchLimit = 1;
NSError *error = nil;
NSArray *results = [context executeFetchRequest:fetch error:&error];
if (results.count > 0) {
return [results objectAtIndex:0];
}
return nil;
}