Search code examples
core-datasavensfetchrequest

What if executeFetchRequest returns 1 item of type: NSKnownKeysDictionary1 instead of managedObjectContext


I have working code that receives a web service of updates, makes the decision to update or create a new, but the update is not working. (I have read dozens of other questions here, including one here that is what I am following. Another example is the accepted answer here)

The saveContext: below executes without error and without query (using Debug level 2)

The returned array with 19 as the desired record. From po results:

<_PFArray 0xa349280>(
{
    address = "an address";
    companyName = "the Name";
    email = "email@company.com";
    fname = "first name";
    inserted = "2014-05-01 04:00:00 +0000";
    level = 3;
    lname = "last name";
    num = 19;
    phone = "phone #";
    tax2 = "tax number";
}

To summarize:

  1. No query to Core Data is being performed at saveContext
  2. No errors are being thrown in debugger or by saveContext
  3. Changes are NOT being saved to Core Data
  4. The results array is a _PFArray
  5. I think the issue is with the existingObject object. It is not correctly referencing the managedObjectContext.

What do I need to have the saveContext save the data?

** EDIT: ** I have narrowed this down to the existObject NOT being the correct type, but am still looking for the reason it isn't a managedObjectContext.

Here is the fetch method. Maybe the issue is in here:

- (NSArray *)performFetchInEntity:(NSString *)entity withPredicate:(NSString *)predicate {
    NSManagedObjectContext *context = self.managedObjectContext;

    // Create the fetch request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity: [NSEntityDescription entityForName:entity
                                         inManagedObjectContext:context]];

    // This next line should NOT be used !!!
    // [fetchRequest setResultType:NSDictionaryResultType];
    [fetchRequest setReturnsObjectsAsFaults:NO];
    if (predicate)
        [fetchRequest setPredicate:[NSPredicate predicateWithFormat:predicate]];

    // Execute the fetch.
    NSError *error;
    NSArray *result = [context executeFetchRequest:fetchRequest
                                             error:&error];

    NSLog(@"returned: %@", [result objectAtIndex:0]);
    if (!error)
        return result;
    else
        return nil;
}

Solution

  • Found the culprit:

    [fetchRequest setResultType:NSDictionaryResultType];
    

    Removing that line caused the type to correctly be returned in the resulting array.