How to get specific records in master data

I am a new bet in iOS. I am using NSManagedObject from Core Data to perform Insert and Fetch operations. It works great. But the problem is that I want to extract from the table only some selected records (where is the condition in MySQL). E.g. "select a name from users, where city = 'Pune'"; I found NSPredicate to get filtered data. But it gives all the data in the array, not just the selected one. E.g. if the result for the request is higher:

Anu

Then the result of NSPredicate will give:

fname = Anu
lname = Padhye
city = Pune
id = 3

Is there a way to only select selected entries in iOS Objective-c? The following is a snippet of the code I'm using for NSManagedObject:

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"User"];
    valueData = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];


NSEntityDescription *productEntity=[NSEntityDescription entityForName:@"User" inManagedObjectContext:managedObjectContext];
NSFetchRequest *fetch=[[NSFetchRequest alloc] init];
[fetch setEntity:productEntity];
NSPredicate *p=[NSPredicate predicateWithFormat:@"id == %d", 3];
[fetch setPredicate:p];
    //... add sorts if you want them
NSError *fetchError;
NSArray *fetchedProducts=[valueData filteredArrayUsingPredicate:p];
+4
1

:

 NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"User"];
 request.predicate = [NSPredicate predicateWithFormat:@"city == %@ && id == %d", @"Pune", 3];
 request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"id" ascending:YES]];

 NSArray *results = [managedObjectContext executeFetchRequest:request error:nil];

, Pune 3.

+14

Source: https://habr.com/ru/post/1547733/


All Articles