0

I have a Core Data in my application. One of the entity is Details. Details have 2 attributes named guid and the other is details. Both attributes contain a lot of values. At the point of fetching, I have to take the details of the corresponding guid. Can I get the values that saved in the other attribute with the guid? How can I connect these two attributes? This is how I am saving values to 2 attributes.

ReaderAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSManagedObject *object;
object = [NSEntityDescription insertNewObjectForEntityForName:@"Details" inManagedObjectContext:context];

[object setValue: [[activity objectForKey:@"category_guid"]stringValue]forKey:@"guid"];
[object setValue:[activity objectForKey:@"details"] forKey:@"details"];

NSError *error;            
if (![context save:&error]) {

    NSLog(@"Error save base");
}
else {

    NSLog(@"saved");                
}
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
iOS Developer
  • 1,723
  • 2
  • 16
  • 47

1 Answers1

1

I'm not sure I understand your question but if you want to select a specific Details object based on a guid you can create a NSFetchRequest with a NSPredicate like the following:

NSFetchRequest* request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Details" inManagedObjectContext:moc]];
[request setPredicate:[NSPredicate predicateWithFormat:@"guid == %@", yourGuid]];

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
if([results count] > 0) {

    // the object you are looking for
    NSManagedObject* grabbedDetail = [results objectAtIndex:0];

    NSString* grabbedDetails = [grabbedDetail valueForKey:@"details"];

    // here you have grabbed details attribute
    // I suppose it's of type NSSString... 
}

// [request release]; if you don't use ARC

Now about your question, could you try to explain what these mean?

Can I get the values that saved in the other attribute with the guid? How can I connect these two attributes?

Hope that helps.

Edit:

If results is zero you could first retrieve objects without the predicate. So, do the following:

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

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
if([results count] > 0) {

    for(NSManagedObject* obj in results) {

        NSLog(@"obj -> %@", obj)
    }
}

// [request release]; if you don't use ARC

If you have no logs, it means you have no data!! On the contrary set the predicate with an hardcoded guid value like:

[request setPredicate:[NSPredicate predicateWithFormat:@"guid == %@", @"12345"]];

You need to be sure the guid is there.

I don't know what the problem is, but the query I used it's correct and with no details I cannot understand what is going on.

Cheers.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • i just tried as u said.but the [results count] is getting as 0 – iOS Developer Sep 28 '12 at 09:12
  • Provide some info about your model. What's the type of `guid`? – Lorenzo B Sep 28 '12 at 11:03
  • If the query returns 0, it means that not object has the `guid` you specified. Try first with an hardcoded `guid` (be sure it exists) and see what happens. e.g. `[NSPredicate predicateWithFormat:@"guid == %@", @"12345"];` – Lorenzo B Sep 28 '12 at 11:05
  • @alpz Remove the predicate and print the result. Are you able to retrieve any objects? I edited the answer to fit this. – Lorenzo B Sep 28 '12 at 12:23