NSFetchRequest *fetchLLObjects = [[NSFetchRequest alloc] init];
[fetchLLObjects setEntity:[NSEntityDescription entityForName:#"CustomerOrder" inManagedObjectContext:self.managedObjectContext]];
[fetchLLObjects setIncludesPropertyValues:NO]; //only fetch the managedObjectID
NSError *error = nil;
NSArray *allObjects = [self.managedObjectContext executeFetchRequest:fetchLLObjects error:&error];
Here array is showing nil. But in database I could see that there are data. I don't know what might be the reason.
Please add your predicate in the fetchrequest
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"CustomerOrder"
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:#"managedObjectID == %#", managedObjectID]] inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
Set following 2 properties and you are done.
fetchRequest.returnsDistinctResults = YES;
fetchRequest.resultType = NSDictionaryResultType;
Hope this helps.
Edit
NSFetchRequest *fetchRequest= [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Songs" inManagedObjectContext:myManagedObjectContext];
//Take properties dictionary
NSDictionary *entityProperties = [entity propertiesByName];
[fetchRequest setEntity:entity];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:#"song_type"]]];
NSArray * result = [myManagedObjectContext executeFetchRequest:fetchRequest error:nil];
You said: "But in database I could see that there are data."
I'm skeptical of that statement. I suspect that no CustomerOrder objects exist in the Core Data store. The only way to "see" the data inside the store is to turn off journaling mode and use 3rd party software to peak inside the .sqlite file.
Despite what others have said, your fetch request is fine. So double check your code where you insert objects into the store and save to the context.
Related
I wanted to update one record in my core data entity and I write code for it but the data is not updated .
There is Entity posts and it has data with id
i have to update the record for one particular id .
Here is function to update the record
-(void)updateDataInDB:(NSString*)withID WithDes:(NSString*)newData{
NSManagedObjectContext * context = [self getManagedContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"News" inManagedObjectContext:context];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"postId == %#",withID];
[fetchRequest setPredicate:predicate];
[fetchRequest setFetchLimit:1];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *arrResult = [context executeFetchRequest:fetchRequest error:&error];
if(arrResult.count>0){
NSManagedObject *news = [arrResult objectAtIndex:0];
[news setValue:newData forKey:#"fullDesc"];
}
[self saveContext];
}
Please Help if anything wrong
My core data contains duplicate records and i updated only first records so that the problem with update. After I updated all array records the code working fine.
Every time my app runs it generates data that is stored in a core data entity "Entity". The result of my fetch request does not update with this most recent data, except if I exit the app and relaunch it again. I thought the "shouldRefreshRefetchedObjects" property would fix this issue but apparently not. My current fetch request code is the following:
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Entity" inManagedObjectContext:context];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"date_time" ascending:YES];
NSArray *Descriptors = [NSArray arrayWithObject: descriptor];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setShouldRefreshRefetchedObjects:YES];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:Descriptors];
NSArray *historyDataArray = [context executeFetchRequest:fetchRequest error:Nil];
I'm having troubles in updating a specific record in my core data. What happens in my code is it does change the value but when I rerun the app it goes back to its original value. Why that happens?
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Wish" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
// Specify criteria for filtering which objects to fetch
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"itemWish = %#", self.wishItemStr];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
[fetchedObjects setValue:#"YES" forKeyPath:#"isAchieved"];
if (fetchedObjects == nil) {
NSLog(#"no fetched objects!");
}
You need to save the context.
[managedObjectContext save:&error];
I suggest you to read these very good tutorials and articles on objc.io.
I have Core Data set up for my app. I ran into a problem with the fetch request, but fixed it by specifying the managedObjectContext being from the appDelegate.
When I do an NSLog, it returns the array count as being 0. Any suggestions on how to debug this? I'm not sure where to start debugging as I'm relatively new.
I know there's data in Core Data, and I think the fetch is going through alright. The entity is named Category, and that's where I'm stuck! Any tips for debugging would be great.
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Category" inManagedObjectContext:[appDelegate managedObjectContext]];
[request setEntity:entity];
// Specify how to sort the list
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"cat_name" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *fetchResults = [managedObjectContext executeFetchRequest:request error:&error];
NSLog(#"%d",[fetchResults count]);
Thanks!
Simple issue was that I was calling [appDelegate managedObjectContext] in the second l line, but was only calling managedObjectContext for the fetchResults.
How do I fetch, say, item 7 000 to 7 999 in my SQLite table with around 100 000 items?
The normal fetch returns to much to work with, and I rather fetch it little by little:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"Log"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&err];
// here fetchedObjects holds around 100 000 rows
Is there a way to specify a range with in executeFetchRequest:fetchRequest?
Do I have to use the SQLite API?
Is it a good idea?
Use the fetchLimit and fetchOffset properties on NSFetchRequest to fetch an object in a selected range.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.fetchLimit = 1000;
fetchRequest.fetchOffset = 7000;
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"Log"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&err];