【发布时间】:2013-09-11 12:13:24
【问题描述】:
- (IBAction)findContact:(id)sender {
CoreDataAppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
// NSManagedObjectContext *context =
// [appDelegate managedObjectContext];
NSManagedObjectContext *managedContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
managedContext = [appDelegate managedObjectContext];
//[context setParentContext:[self managedObjectcontext]];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:@"Contacts"
inManagedObjectContext:managedContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred =
[NSPredicate predicateWithFormat:@"(name == %@)",
_name.text];
[request setPredicate:pred];
[managedContext performBlock:^{
NSError *error = nil;
NSArray *fetchedObjects = [managedContext executeFetchRequest:request error:&error];
_objects = fetchedObjects;
NSManagedObject *matches = nil;
NSLog(@"data= %@", [_objects description]);
if (_objects == nil) {
_status.text = @"No matches";
} else {
matches = _objects[0];
_address.text = [_objects valueForKey:@"address"];
_phone.text = [_objects valueForKey:@"phone"];
_status.text = [NSString stringWithFormat: @"%d matches found", [_objects count]];
}
}];
}
【问题讨论】:
标签: iphone core-data background fetch