enter image description here

There is an entities where I am filling data from JSON, Say it is Photographer and Photo. Both have some data which I have filled using loop and managed ObjectContex ..

Like this,

NSMutableArray *ArrPhotographer= [[self.M3Arr objectForKey:@"LifeMag"]objectForKey:@"Photographer"];

for (int i = 0; i< ArrPhotographer.count; i++) {
 Photographer *  photographerObj = [NSEntityDescription insertNewObjectForEntityForName:@"PhotographerData"
                                               inManagedObjectContext:[self managedObjectContext]];
    NSMutableDictionary *tpDict = [cleanerListArr objectAtIndex:i];
    photographerObj.cleanerName = [tpDict objectForKey:@"photographerName"];
}

Now I have done this for both Photographer and Photo Entity and as per this picture my Magazine entity is having data which are already existed in this table . And as shown I have made to one relation to both Photo and Photographer from Magazine .

Now Question is ,

If Photographer Name is already existed in the table , How can I connect it with Magazine Entity . I need the managed object Reference of that particular place .

(For Example , There are three photographer , Ron , Harry and Sunny now For Photo Cover1 I want name of Ron . then I need the Object Reference of Ron when I pre Populate it).

How to get this object Reference?

// **************** EDIT

I am getting the object is present ...but No how to fetch object and 2) how to give it to x and y stated above ?

I am using this code to saving the data in Magazine

    Magazine *magObj = [NSEntityDescription insertNewObjectForEntityForName:@"Magazine"
                                                    inManagedObjectContext:[self managedObjectContext]];

    magObj .issueID=[NSNumber numberWithInt:1];
    magObj .photo= x;
    magObj .photographer = y;
#### Edit 2
   NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Cleaner"];
    [request setPredicate:[NSPredicate predicateWithFormat:@"cleanerName = %@", @"Robin"]];
    [request setFetchLimit:1];
    NSUInteger count = [[self managedObjectContext] countForFetchRequest:request error:nil];
    if (count == NSNotFound){

        NSLog(@"ERROR FOund");
    }
    // some error occurred
    else if (count == 0){

        NSLog(@"no matching object");
    }
    // no matching object
    else{

        NSLog(@"Found Match");

    }
有帮助吗?

解决方案

You need to use an NSFetchRequest to search the context for the appropriate object to connect to. The fetch request specifies the entity type to search for and you need to add an NSPredicate to filter the results to only the particular name that you're interested in.

Note that you could run a single fetch request with a list of names so that you only hit the data store for information once and then use the returned list during your object creation / connection loop.

If you're loading all of your data in one go, then you can create a dictionary which contains the managed object instances such that you can link to them without fetching.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top