Reputation: 10398
I am wanting to add URL schemes to my iOS app, however the URL needs to be able to point towards a certain NSManagedObject
from Core Data
. I'm quite happy for my app to have to generate the URL for the user to use, but it just doesn't seem right to use the whole NSManagedObject
URI in the URL.
When I retrieve the URI of the managed object, it is like this:
x-coredata://633EAF37-A03D-4954-976D-B3B0C32F8033/MyObject/p7
I'm guessing I can drop the x-coredata://
part which I can put back in my application:openURL
method, but this still leaves me with a URL like this:
myurlscheme://event_to_perform?object=633EAF37-A03D-4954-976D-B3B0C32F8033/MyObject/p7
Is there more I can do to shorten this?
What about the has part 633EAF37-A03D-4954-976D-B3B0C32F8033
? Will this be the same across every device the app is installed on, or is it unique?
If it was the same across devices then i'd only really need to use the final p7
as everything else I could add back in a string.
Any advice appreciated.
Thanks
Upvotes: 1
Views: 1375
Reputation: 81
That feels like exposing your implementation too much. I would highly recommend you maintain your own unique id property in the entity and use it to fetch the right entity from CoreData when you get a URL lookup.
This future proofs you should you ever, say, start syncing to a web-based version of your app or some other data store that is not CoreData.
Upvotes: 0
Reputation: 539845
Perhaps have a look at Permanent NSManagedObjectID not so permanent? first about the fragility of passing a NSManagedObjectID
around. Marcus S. Zarra claims that the objectID
can change during the life of a object.
That being said, the URI for a permanent managed object id seems always to be built like this:
x-coredata://<UUID>/<EntityName>/p<Key>
where
<UUID>
is the NSStoreUUIDKey
value of the metadata
dictionary of NSPersistentStore
,<EntityName>
is the entity name :-)<Key>
is the primary key that SQLite uses internally for the table (but which is invisible
to the Core Data API).But note that this is only what I observed. The format is not documented and can probably change at any time.
The <UUID>
is generated when a store file is created, so it is not the same across every device the app is installed on.
So if the above analysis is correct and the URI scheme does not change in the future,
then you could indeed reconstruct the managed object URI from the final component p<Key>
alone.
Upvotes: 4