开发者

NSManagedObjectContext's registeredObjects won't return anything unless a fetch is executed beforehand

Cheers,

I'm experiencing a problem with core data, I guess I'm just looking in the wrong direction again.

My managedObjectContext will return an empty NSSet if I call registeredObjects on it. If I execute a fetch beforehand however, it will return the same objects that as the fetch did just a moment ago.

There's no multithreading going on.

Here's what I do:

[self setupContext]; // This will set up managedObjectContext, which is 开发者_JS百科a property of this class

// Fetching...    

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *foo = [NSEntityDescription entityForName:@"Foo" inManagedObjectContext:managedObjectContext];
[request setEntity:foo];

NSError *fetchError = nil;
NSArray *fetchResults = [managedObjectContext executeFetchRequest:request error:&ftchError];

NSLog(@"Fetch returned %i objects.", [fetchResults count]);

[request release];

// Done fetching...

NSArray *allObjects = [[managedObjectContext registeredObjects] allObjects];

NSLog(@"Context contains %i objects...", [allObjects count]);

The store contains 30 objects. If I run the code above, both NSLogs will report five objects. If I remove the fetch part between the two comments, it will report zero objects for the whole context.

Note that I am at no point commiting or otherwise changing the contexts contents.

Do I need to force the context into refreshing itself first? I've never done this before though and I don't recall registeredObjects failing on me like this on other occasions in the first place.

Any suggestions appreciated! Toastor


You may be confused about what registeredObjects means. This is the set of objects that are currently in the NSManagedObjectContext. This is not the set of objects in the store, just the ones in the context. If you haven't fetched or otherwise registered the objects in the context, then they won't be in registeredObjects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜