问题
my sqlite have items in one of the entity and i wish to retrieve them out according to their categoryID
e.g. categoryID 1 have 7 items and categoryID 2 have 5 items , when i clicked on an IBAction it will come to this method and retrieve according to my global.clickedTag
but i'm unable to retrieve the values , any idea why?
-(NSMutableArray*)fetchItem:array{
[self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"IItem" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
//filter here
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"categoryID == %i",myGlobal.clickedTag];
[request setPredicate:predicate];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ID" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
NSError *error;
NSMutableArray* mutableFetchCategory = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (!mutableFetchCategory) {
// Handle the error.
}
if ([mutableFetchCategory count] > 0)
{
for (NSManagedObject *info in mutableFetchCategory)
{
if ([[info valueForKey:@"categoryID"]intValue] == myGlobal.clickedTag)
{
NSLog(@"ID: %@", [info valueForKey:@"ID"]);
NSLog(@"category NAME/photo: %@", [info valueForKey:@"name"]);
NSLog(@"categoryID : %@",[info valueForKey:@"categoryID"]);
Item * i = [[Item alloc]initWithTitle:[info valueForKey:@"ID"] name:[info valueForKey:@"name"] description:[info valueForKey:@"desc"] thumbnail:[info valueForKey:@"thumbnail"] photo:[info valueForKey:@"photo"] categoryID:[info valueForKey:@"categoryID"]];
[array addObject:i];
NSLog(@"array count : %i",[array count]);
}
}
}
[mutableFetchCategory release];
[request release];
return array;
}
回答1:
You have a lot of problems:
This line:
-(NSMutableArray*)fetchItem:array{
... is not a proper method definition. It should look like:
-(NSMutableArray*)fetchItem:(NSMutableArray *) array{
The next line:
[self managedObjectContext];
... does nothing except call the accessor but then the return is ignored. The compiler will complain about that and you shouldn't ignore it.
Your major problem is here:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"IItem" inManagedObjectContext:managedObjectContext];
... because you don't use the self.managedObjectContext
or [self managedObjectContext]
to access the context. That makes it likely you will get a nil context or one you don't want. You make the same mistake here:
NSMutableArray* mutableFetchCategory = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
BTW, you don't need a mutable array here and you don't want to copy it. You will end up with duplicate managed objects which will cause all kinds of problems. There was some reference material somewhere that used this form but it has since been corrected.
The entire block that starts with:
if ([mutableFetchCategory count] > 0)
is pointless. It just creates further duplication of an already copied array.
You are making this a lot more complicated than it needs to be. Just use the "Core Data Fetch With Predicate` snippet in Xcode4 and fill in the blanks.
-(NSArray *) fetchIItemsWithCategoryID:(NSNumber *) catID{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"IItem"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"categoryID==%@",
catID];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"ID" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSError *error = nil;
NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
//... error handling code
}
[fetchRequest release];
return fetchedObjects; //fetchedObjects will always exist although it may be empty
}
fetchedObjects
will be an array of either NSManagedObject objects or IItem
objects depending on on whether you provided a custom NSManagedObject subclass or not.
来源:https://stackoverflow.com/questions/7020104/fetching-core-data-values-using-predicate