CoreData: Same predicate (IN) returns different fetched results after a Save operation

Posted by Jason Lee on Stack Overflow See other posts from Stack Overflow or by Jason Lee
Published on 2014-06-06T15:28:00Z Indexed on 2014/06/07 21:25 UTC
Read the original article Hit count: 256

Filed under:
|
|
|

I have code below:

        NSArray *existedTasks = [[TaskBizDB sharedInstance] fetchTasksWatchedByMeOfProject:projectId];
        [context save:&error];
        existedTasks = [[TaskBizDB sharedInstance] fetchTasksWatchedByMeOfProject:projectId];

        NSArray *allTasks = [[TaskBizDB sharedInstance] fetchTasksOfProject:projectId];
  • First line returns two objects;
  • Second line save the context;
  • Third line returns just one object, which is contained in the 'two objects' above;
  • And the last line returns 6 objects, containing the 'two objects' returned at the first line.

The fetch interface works like below:

WXModel *model = [WXModel modelWithEntity:NSStringFromClass([WQPKTeamTask class])];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%@ IN personWatchers) AND (projectId == %d)", currentLoginUser, projectId];
[model setPredicate:predicate];
NSArray *fetchedTasks = [model fetch];
if (fetchedTasks.count == 0) return nil;

return fetchedTasks;

What confused me is that, with the same fetch request, why return different results just after a save?

Here comes more detail:

The 'two objects' returned at the first line are:

<WQPKTeamTask: 0x1b92fcc0> (entity: WQPKTeamTask; id: 0x1b9300f0 <x-coredata://CFFD3F8B-E613-4DE8-85AA-4D6DD08E88C5/WQPKTeamTask/p9> ; data: {
    projectId = 372004;
    taskId = 338001;
    personWatchers =     (
        "0xf0bf440 <x-coredata://CFFD3F8B-E613-4DE8-85AA-4D6DD08E88C5/WWPerson/p1>"
    );
}

<WQPKTeamTask: 0xf3f6130> (entity: WQPKTeamTask; id: 0xf3cb8d0 <x-coredata://CFFD3F8B-E613-4DE8-85AA-4D6DD08E88C5/WQPKTeamTask/p11> ; data: {
    projectId = 372004;
    taskId = 340006;
    personWatchers =     (
        "0xf0bf440 <x-coredata://CFFD3F8B-E613-4DE8-85AA-4D6DD08E88C5/WWPerson/p1>"
    );
}

And the only one object returned at third line is:

<WQPKTeamTask: 0x1b92fcc0> (entity: WQPKTeamTask; id: 0x1b9300f0 <x-coredata://CFFD3F8B-E613-4DE8-85AA-4D6DD08E88C5/WQPKTeamTask/p9> ; data: {
    projectId = 372004;
    taskId = 338001;
    personWatchers =     (
        "0xf0bf440 <x-coredata://CFFD3F8B-E613-4DE8-85AA-4D6DD08E88C5/WWPerson/p1>"
    );
}

Printing description of allTasks:

<_PFArray 0xf30b9a0>(
<WQPKTeamTask: 0xf3ab9d0> (entity: WQPKTeamTask; id: 0xf3cda40 <x-coredata://CFFD3F8B-E613-4DE8-85AA-4D6DD08E88C5/WQPKTeamTask/p6> ; data: <fault>),
<WQPKTeamTask: 0xf315720> (entity: WQPKTeamTask; id: 0xf3c23a0 <x-coredata://CFFD3F8B-E613-4DE8-85AA-4D6DD08E88C5/WQPKTeamTask/p7> ; data: <fault>),
<WQPKTeamTask: 0xf3a1ed0> (entity: WQPKTeamTask; id: 0xf3cda30 <x-coredata://CFFD3F8B-E613-4DE8-85AA-4D6DD08E88C5/WQPKTeamTask/p8> ; data: <fault>),
<WQPKTeamTask: 0x1b92fcc0> (entity: WQPKTeamTask; id: 0x1b9300f0 <x-coredata://CFFD3F8B-E613-4DE8-85AA-4D6DD08E88C5/WQPKTeamTask/p9> ; data: {
    projectId = 372004;
    taskId = 338001;
    personWatchers =     (
        "0xf0bf440 <x-coredata://CFFD3F8B-E613-4DE8-85AA-4D6DD08E88C5/WWPerson/p1>"
    );
}),
<WQPKTeamTask: 0xf325e50> (entity: WQPKTeamTask; id: 0xf343820 <x-coredata://CFFD3F8B-E613-4DE8-85AA-4D6DD08E88C5/WQPKTeamTask/p10> ; data: <fault>),
<WQPKTeamTask: 0xf3f6130> (entity: WQPKTeamTask; id: 0xf3cb8d0 <x-coredata://CFFD3F8B-E613-4DE8-85AA-4D6DD08E88C5/WQPKTeamTask/p11> ; data: {
    projectId = 372004;
    taskId = 340006;
    personWatchers =     (
        "0xf0bf440 <x-coredata://CFFD3F8B-E613-4DE8-85AA-4D6DD08E88C5/WWPerson/p1>"
    );
})
)

UPDATE 1

If I call the same interface fetchTasksWatchedByMeOfProject: in:

#pragma mark - NSFetchedResultsController Delegate

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{

I will get 'two objects' as well.

UPDATE 2

I've tried:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY personWatchers == %@) AND (projectId == %d)", currentLoginUser, projectId];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY personWatchers.personId == %@) AND (projectId == %d)", currentLoginUserId, projectId];

Still the same result.

UPDATE 3

I've checked the save:&error, error is nil.

© Stack Overflow or respective owner

Related posts about ios

Related posts about objective-c