Here I got the value in fetchedObjects. How can I update the value in the array?
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Reserve" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
//NSSortDescriptor tells defines how to sort the fetched results
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"number" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSLog(#"sort count%d",[sortDescriptors count]);
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:#"Root"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(number = 100)", self.txtReserve.text];
// [fetchRequest setPredicate:predicate];
[fetchedResultsController.fetchRequest setPredicate:predicate];
self.fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSLog(#"Count of array::::%d",[fetchedObjects count]);
NSManagedObject *device = [fetchedObjects objectAtIndex:0];
NSLog(#"device::::%#",device);
I suggest that you read/watch some of the many books/tutorials on Core Data. You are missing a very basic understanding of the technology.
First, the objects you receive represent objects in the database, you have to know the entity descriptions to get the data. For example, the most basic way to get the "number" attribute of the "Reserve" entity would be...
[managedObject valueForKey:#"number"];
If you use Xcode (or a tool called mogenerator) you can have subclasses generated for your entities, and can more easily access the data via custom accessor methods.
Related
i used this code to select all products that related to specific category but it doesn't work ,, what is the correct predicate that i can use to get all products related to this category
NOTE:
i get the categories list from server alone and insert them ,, and then get products list from server and inserting them ,, but i figured out now that the product don't know his category ,, i'm using restkit library to parse and insert in the database ,, so how i can tell the products that the category ,, the restkit do all the work automatically
how to set to the product list thet i get from server their category ?
//--fetching inserted Results from core data
// Getting products
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"productId" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor , nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// NSPredicate *predicate = [NSPredicate predicateWithFormat:#"category = %# ",tempCategoryHolder];
// [fetchRequest setPredicate:predicate] ;
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Product"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
fetchedObjectsProducts = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
I would do it like this, assuming tempCategoryHolder holds a categoryId:
NSError *error = nil;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"Product"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:#"category.categoryId = %#", tempCategoryHolder];
fetchRequest.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"productId" ascending:YES]];
NSArray *fetchedObjectsProducts = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
Otherwise update the predicate to refer to category object, or different category attribute.
Using below code, I am fetching an array of emails from Core Data. I pass an array of emailId as the predicate to the fetch and it returns all emails for each of the emailIds.
However, I only need last object (or last email) of each of the emailIds in the predicate. Currently, I take the output of this fetch and use separate sorting to filter the objects I want. However, I would like the filtering in this fetch itself. Is there any way to do that?
- (NSFetchedResultsController *)getLatestEmail:(NSArray *)emailId
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"email_id IN %#", emailId];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Emails"
inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"last_update_timestamp" ascending:NO];
NSArray *sortDescriptors = #[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:self.context
sectionNameKeyPath:nil
cacheName:#"Master"];
aFetchedResultsController.delegate = self;
self.emailFetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.emailFetchedResultsController performFetch:&error])
{
NSLog(#"getLatestEmail failed %#, %#", error, [error userInfo]);
}
NSArray *emails = [[self.context executeFetchRequest:fetchRequest error:&error] mutableCopy];
return _emailFetchedResultsController;
}
I tried adding .lastobject to [[self.context executeFetchRequest:fetchRequest error:&error] mutableCopy], but didn't work. I couldn't also find any documentation that suggests that this is possible. However, I am sure, someone would have tried it before and would like to hear their opinion.
Invert your sort descriptor, then just set the batch size to 1 and the offset to zero.
For better understanding my problem imagine your facebook friends; I have one entity "user" and I use that for displaying relationship between friends;
I don't know how to get friends of concrete user using NSFetchRequest
I have two "to many" relationship(see above) and I try to get friends using NSPredicate, but in the end array is empty.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = (NSEntityDescription *)[_dbManager userEntity];
[fetchRequest setEntity:entity];
User *userByUid = [_dbManager userByUid:_currentUid];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:#"sortId" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(friends IN %#)", userByUid];
[fetchRequest setPredicate:predicate];
[fetchRequest setFetchBatchSize:10];
NSArray *array = [_fetchedResultsController.managedObjectContext executeFetchRequest:fetchRequest error:nil];
You have to use the inverse relationship and "ANY":
[NSPredicate predicateWithFormat:#"ANY beFriendsWith == %#", userByUid]
in my app I am sorting an entity using only one NSSortDescriptor, but now I need to filter the data for 4 different attributes. This is the code for the current sorting, the result are all records from the entity sorted by the attribute 'displayOrder', but I need to filter the records by four other attributes(year,month,day,group), all of them integer32 except group which is string :
- (NSFetchedResultsController *)fetchedResultsController
{
if (fetchedResultsController) return fetchedResultsController;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity =
[NSEntityDescription entityForName:#"FavoriteThing"
inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor =
[[NSSortDescriptor alloc] initWithKey:#"displayOrder"
ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc]
initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:nil cacheName:#"ThingsCache"];
aFetchedResultsController.delegate = self;
[self setFetchedResultsController:aFetchedResultsController];
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
return fetchedResultsController;
}
I have tried adding a predicate:
NSNumber *yearSearched = 1965;
NSPredicate *yearPredicate = [NSPredicate predicateWithFormat:#"todoYear = %#", yearSearched];
[fetchRequest setPredicate:yearPredicate];
But after running the app there is an error EXC_BAD_ACCESS at the line NSPredicate *yearPredicate = [NSPredicate predicateWithFormat:#"todoYear = %#", yearSearched];
Where the attribute 'todoYear' is from type Integer32
Your NSNumber creation is wrong (you're just setting the pointer to an integer). It should be:
NSNumber *yearSearched = #1965;
NSPredicate *yearPredicate = [NSPredicate predicateWithFormat:#"todoYear = %#", yearSearched];
[fetchRequest setPredicate:yearPredicate];
I have a problem with Core Data. I read around that Core Data is lazy to load entity relationships, in case this, has not a to-many relationship.
I have an entity "Item" which has a relationship (to-many relationship) with an entity "Movement".
In this method, addition to loading all instances of "Movement", I want to load the relative attributes of "Item", for example "name".
//Core data request
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Movement"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"date" ascending:YES selector:#selector(localizedCaseInsensitiveCompare:)]];
NSFetchedResultsController *result = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil
cacheName:#"Root"];
[result performFetch: &error];
to see if it works, I added this code:
NSArray *array = [NSArray arrayWithArray:[self.managedObjectContext executeFetchRequest:fetchRequest error:&error]];
for (int i = 0; i < array.count; i++) {
Movement *movement = [array objectAtIndex:i]
NSLog(#"item name %#",movement.movement_inItem.name);
}
the result is null item name (null)
I looked around, and tried to put this code under [fetchRequest setEntity:entity];
NSString *relationshipKeyPath = #"movement_inItem";
NSArray *keyPaths = [NSArray arrayWithObject:relationshipKeyPath];
[fetchRequest setRelationshipKeyPathsForPrefetching:keyPaths];
[fetchRequest setIncludesSubentities:YES];
but nothing changes.
Thanks a lot!