使用谓词获取核心数据值
我的 sqlite 在其中一个实体中有项目,我希望根据其类别 ID 检索它们
例如,categoryID 1 有 7 个项目,categoryID 2 有 5 个项目,当我单击 IBAction 时,它将进入此方法并根据我的 global.clickedTag 进行检索
但我无法检索这些值,知道为什么吗?
-(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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有很多问题:
这一行:
...不是正确的方法定义。它应该看起来像:
下一行:
... 除了调用访问器之外什么都不做,但返回被忽略。编译器会抱怨这一点,你不应该忽略它。
您的主要问题在这里:
...因为您不使用
self.managementObjectContext
或[self ManagedObjectContext]
来访问上下文。这使得您可能会得到一个零上下文或您不想要的上下文。您在这里犯了同样的错误:顺便说一句,您在这里不需要可变数组,并且您不想复制它。您最终将得到重复的托管对象,这将导致各种问题。某处有一些参考材料使用了这种形式,但后来已被更正。
以:开头的整个块
是毫无意义的。它只是对已经复制的数组创建进一步的重复。
你让事情变得比需要的复杂得多。只需在 Xcode4 中使用“Core Data Fetch With Predicate”片段并填写空白即可。
fetchedObjects
将是 NSManagedObject 对象或IItem
对象的数组,具体取决于您是否是否提供了自定义 NSManagedObject 子类。You have a lot of problems:
This line:
... is not a proper method definition. It should look like:
The next line:
... 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:
... 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: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:
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.
fetchedObjects
will be an array of either NSManagedObject objects orIItem
objects depending on on whether you provided a custom NSManagedObject subclass or not.