WORKAROUND: How to create two separate in-memory stores?
WORKAROUND: How to create two separate in-memory stores?
- Subject: WORKAROUND: How to create two separate in-memory stores?
- From: Matthew Firlik <email@hidden>
- Date: Thu, 28 Jul 2005 16:10:39 -0700
As a follow-up, there *is* a workaround to this issue. If you pass a
valid *file* URL as the argument to the NSPersistentStoreCoordinator
when adding an in-memory store, the addition of the second (and all
subsequent) in-memory stores will succeed.
Obviously, the file from the URL will NOT be used as the backing for
the store: it's just needed to bypass the validity checking code for
the URL. The framework checks on the URL to ensure it is of the
proper format and represents something which is or can be a file on
disk. The URL does not need to point to an existing file, but it must:
- reside within a directory structure that already exists
- must be in a directory that is writeable
- not be a directory (must be a file)
You do not need to use a unique URL for each memory store you add:
you can use the same URL for each. So, the code for this workaround
might resemble something like:
= = = = =
// create a new model and coordinator
NSManagedObjectModel *model = [NSManagedObjectModel
mergedModelFromBundles:
[NSArray arrayWithObject: [NSBundle mainBundle]]];
NSPersistentStoreCoordinator *coordinator =
[[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel: model];
// create a temp URL
NSURL *tempURL = [NSURL fileURLWithPath: @"/tmp/randomFileName"];
// add a memory store
NSError *error;
id store = [coordinator
addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil URL:tempURL options:nil error:&error];
if ( store ) {
// add another one
id anotherStore = [coordinator
addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil URL:tempURL options:nil error:&error];
if ( anotherStore ) {
// log
NSLog( @"success");
}
// log
else NSLog( @"error adding second store: %@", [error
localizedDescription] );
}
// log
else NSLog( @"error adding first store: %@", [error
localizedDescription] );
// clean up
[coordinator release];
= = = = =
This is a workaround for those who need this functionality before the
underlying bug is addressed.
- matthew
On Jul 27, 2005, at 12:33 PM, Matthew Firlik wrote:
Frank et al:
This is definitely a bug: the problem is the identifier for the in-
memory store is not being initialized correctly (the URI is
returning "memory://(null)", instead of something unique) which the
coordinator takes as an attempt to add the same store twice.
Unfortunately, there is no workaround for this (other than using
one coordinator per memory store.) We'll fix this as soon as
possible.
- matthew
= = = = =
Matthew Firlik
Manager, Core Data
email@hidden
On Jul 27, 2005, at 9:57 AM, Frank Illenberger wrote:
Two points:
1. Using a separate persistent store coordinator is not an option
because I have to be able to get objects from both stores into one
managed object context
2. There is no limitation to the number of stores you can use with
one PSC. I am successfully using three separate SQLite stores on
one PSC. It just does not work with more than one in-memory store.
Am 27.07.2005 um 18:45 schrieb SA Dev:
Someone please correct me if I'm wrong here (I've not tried this
myself), but the way I understand it is that you'd have to create
a separate NSPersistentStoreCoordinator. I *think* this is
because you can only have one store of any given type in the
coordinator. Again, I may be dead wrong.
On Jul 27, 2005, at 12:25 PM, Frank Illenberger wrote:
Sure, I tried the following:
NSPersistentStoreCoordinator *psc = [moc
persistentStoreCoordinator]; id storeA = [psc
addPersistentStoreWithType:NSInMemoryStoreType configuration:nil
URL:nil options:nil error:nil];
id storeB = [psc
addPersistentStoreWithType:NSInMemoryStoreType configuration:nil
URL:nil options:nil error:nil];
Which produces the "Can't add the same store twice" exception.
And I tried:
id storeA = [psc
addPersistentStoreWithType:NSInMemoryStoreType configuration:nil
URL:[NSURL urlWithString:@"storeA"] options:nil error:nil];
id storeB = [psc
addPersistentStoreWithType:NSInMemoryStoreType configuration:nil
URL:[NSURL urlWithString:@"storeB"] options:nil error:nil];
Which throws the exception "Can't find table for entity Person
in database at URL storeA" and actually creates two files named
"storeA" and "storeB".
Am 27.07.2005 um 18:21 schrieb SA Dev:
Frank:
It all depends on what you mean by "... try to add a second
store with the same call ...". That's a pretty big question.
Actual code, please? :-)
On Jul 27, 2005, at 11:57 AM, Frank Illenberger wrote:
Hi,
does anybody know if it is possible to use two separate in
memory stores with one persistent store coordinator?
I use the following code to create an in-memory store:
NSPersistentStoreCoordinator *psc = [moc
persistentStoreCoordinator];
id store = [psc
addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil URL:nil options:nil error:nil];
This works fine. But if I try to add a second store with the
same call, I get the following exception: "Can't add the
same store twice"
So I thought, the two stores needed separate urls identifying
them. But when I start putting names into the URL like in the
following line
id store = [coordinator
addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil URL:[NSURL URLWithString:@"storeA"]
options:nil error:nil];
I get the following error: "Can't find table for entity Person
in database at URL storeA"
So how do I create two separate in-memory stores in one PSC?
Cheers
Frank
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden