When i search for saved data from core data, it is giving me old saved data while retrieving it. Need help to find my mistake
- (void)retrieveDataFromCoreData
{
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"VitaminData" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSError *error = nil;
NSArray *objects = [context executeFetchRequest:request
error:&error];
NSString *matchFound = nil;
matches = [[NSMutableArray alloc]init];
if ([objects count] == 0) {
//matchFound = @"No matches";
} else {
matches = objects[0];
matchFound = [matches valueForKey:@"userdetails"];
}
NSLog("retrieved data : %@",matchFound);
}
Your fetch request has no predicate. That will cause it to fetch all of the VitaminData
entities in your datastore.
Give it a predicate, and (probably) also some sort descriptors. Then iterate over the results. Note you should also check that objects
is not nil
after your -executeFetchRequest:
. If objects
is nil
, there's an error in your fetch request, and you should look at the contents of error
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.