Frage

It's possible what I'm trying to do can't be done with Core Data so, if so, please let me know.

I have a ManagedObjectContext and I'm bringing in sqlite databases by adding them through addPersistentStoreWithType . . .

So I'll add in some number of databases - let's say between 1 and 5 and then I perform fetches on data in the databases. It's great that I don't have to say what sources I'm searching through - results just come back and they're merged together.

However, what if I WANT to know what Store each result came out of? Is that even possible? Am I thinking too "old school" like I want to know what "files" the results were found?

Help / advice would be appreciated.

Here's some code:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"PMBEntity"
    inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                    initWithKey:@"Name" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[fetchRequest setReturnsDistinctResults:YES];
[sortDescriptor release];

NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

See, above I have a PMBEntity which is a Core Data NSManagedObjectModel I created through XCode. As you can tell, one (of the many) fields is "Name". Here I'm searching for all unique Names and sorting by Name.

What is returned to me is an NSArray of these PMBEntity objects.

War es hilfreich?

Lösung

You want the persistentStore attribute of the managed object's objectID attribute. That will provide you a URL to the persistent store holding the managed object.

However, that is only after the object has been saved at least once to the persistent store. Prior to that it has a temporary UUID that doesn't point to a store. I'm not sure how it is effected by the new imported stores. Probably gets an permanent ID when read in but I haven't test it yet.

Andere Tipps

Well, when when you fetch, you have NSManagedObject instances as the result. The NSManagedObject instance has a reference to its NSManagedObjectContext. That NSManagedObjectContext has a reference to its persistentStoreCoordinator. The NSPersistentStoreCoordinator instance has a reference to the persistent store and the NSManagedObjectModel.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top