fetch request for entity.attribute == @“somevalue”

折月煮酒 提交于 2019-12-03 13:28:33
Lorenzo B

Setting up a NSFetchRequest is equivalent to a SELECT statetement in SQL language.

Here a simple example:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"EntityName" inManagedObjectContext:moc]];

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];

// error handling code

The array results contains all the managed objects contained within the sqlite file. If you want to grab a specific object (or more objects) you need to use a predicate with that request. For example:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"attribute == %@", @"Some Value"];
[request setPredicate:predicate]; 

In this case results contains the objects where attribute is equal to Some Value. Setting a predicate is equal to put the WHERE clause in a SQL statement.

Note

I suppose that the name of the entity is EntityName and its property is called attribute which is of type string.

For further info I suggest you to read the Core Data programming guide and NSFecthRequest class reference.

Hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!