Here is a brief sequence that involves a Core Data stack with an (initially empty) NSManagedObjectContext
and a NSPersistentStoreCoordinator
with two NSPersistentStore
s:
Player *player = [NSEntityDescription insertNewObjectForEntityForName: @"Player"
inManagedObjectContext: context];
player.playerID = playerID;
// (1) [context assignObject: player toPersistentStore: secondStore];
NSFetchRequest *requestForPlayer = [NSFetchRequest fetchRequestWithEntityName: @"Player"];
requestForPlayer.predicate = [NSPredicate predicateWithFormat: @"playerID == %@", playerID];
NSAssert(requestForPlayer.affectedStores == nil, nil); // inits to nil
// (2) requestForPlayer.affectedStores = @[ secondStore ];
players = [context executeFetchRequest: requestForPlayer error: &error];
This fetches one player. If I uncomment (2
) (i.e. involve the second store) it fetches zero (sic!) players. If I uncomment also (1)
it fetches again one player.
So it seems as if the behavior NSFetchRequest
returns unsaved NSManagedObject
s only if
- either
affectedStores
isnil
and those objects have not been assigned to any store - or
affectedStores
is notnil
and those objects have been assigned to an affected store
Is this indeed the case and if so, where is this documented by Apple?