Core Data: Get Managed Object by unique part of the objectID

In my Core Data application, I need to get the pointers to different managed objects whose CoreData objectIDs end with the number I provide. e.g.:

Get the Person object whose objectID ends with unique number 317. The whole ID is:
<x-coredata://A3EDF6C1-229D-4658-A04E-598ADF1C749A/Person/p317>

or I need to get Department object, which ends with 5; again full ID is:
<x-coredata://A3EDF6C1-229D-4658-A04E-598ADF1C749A/Department/p5>

These numbers are actually primary keys in the SQLite database's tables and I can see them if I open the database directly with sqlite3, and I can of course select the records using SQL query;

*but I can not figure out how to get these objects using CoreData. I guess I should create the NSPredicate for the fetch request; but I don't know how to write the predicate which will give me the exact object based on the unique part of the objectID*

As I understand, objectIDs are not object's "normal" attributes.

Please help. Thanks in advance.

MacBook, iMac, iPhone, Mac OS X (10.6.4)

Posted on Nov 14, 2010 9:53 PM

Reply
7 replies

Nov 15, 2010 5:51 AM in response to DavidMan

DavidMan wrote:
*but I can not figure out how to get these objects using CoreData. I guess I should create the NSPredicate for the fetch request; but I don't know how to write the predicate which will give me the exact object based on the unique part of the objectID*


I think you are supposed to consider the entire ID to be unique. You can retrieve the associated object using "objectWithID:" from NSManagedObjectContext.

Nov 15, 2010 8:15 AM in response to etresoft

etresoft wrote:
DavidMan wrote:
*but I can not figure out how to get these objects using CoreData. I guess I should create the NSPredicate for the fetch request; but I don't know how to write the predicate which will give me the exact object based on the unique part of the objectID*


I think you are supposed to consider the entire ID to be unique. You can retrieve the associated object using "objectWithID:" from NSManagedObjectContext.


The problem is that I am parsing the text from another application and I have only the "unique part" at hand and not the whole objectID. So, I need to cope with that.

What I see is that the IDs of every entity start with the same text, e.g. here are the IDs of two different entities:
<x-coredata://A3EDF6C1-229D-4658-A04E-598ADF1C749A/Person/p317>
<x-coredata://A3EDF6C1-229D-4658-A04E-598ADF1C749A/Department/p5>

As you see, the difference is only in the end, after the entity names "Paper" and "Department". But this is my case and I am not completely sure that this will hold true for EVERY future installation on any system for every entity.

*Question: will the part like <x-coredata://A3EDF6C1-229D-4658-A04E-598ADF1C749A be constant for the particular installation, for every entity/object?*

If YES, then I will:
- after getting any/first object, extract the non-unique part, i.e. <x-coredata...749A which is the same for every object in this particular installation on this particular computer
- get the unique part, i.e. /Person/p317, /Person/p23, /Department/p5 etc.
- synthesize the "real ID" by appending these two strings
- ask managedObjectContext to return the object with this ID
- continue with the acquired object

How does it sound? Will this work on other computers?
P.S. FYI: the coredata of this program has only one store i.e. all the data are stored in single sqlite file.

Nov 15, 2010 11:00 AM in response to DavidMan

DavidMan wrote:
The problem is that I am parsing the text from another application and I have only the "unique part" at hand and not the whole objectID. So, I need to cope with that.


That sounds risky to me.

*Question: will the part like <x-coredata://A3EDF6C1-229D-4658-A04E-598ADF1C749A be constant for the particular installation, for every entity/object?*

If YES, then I will:
- after getting any/first object, extract the non-unique part, i.e. <x-coredata...749A which is the same for every object in this particular installation on this particular computer
- get the unique part, i.e. /Person/p317, /Person/p23, /Department/p5 etc.
- synthesize the "real ID" by appending these two strings
- ask managedObjectContext to return the object with this ID
- continue with the acquired object

How does it sound? Will this work on other computers?


What are you actually trying to accomplish here? The big picture? It sure doesn't sound right.

Nov 15, 2010 12:26 PM in response to etresoft

etresoft wrote:
DavidMan wrote:

What are you actually trying to accomplish here? The big picture? It sure doesn't sound right.


*"The big picture":*
- There is a core data application (not mine, let's call it "foreign") with object in interest
- Foreign app has a tableview
- After drag and drop from the tableview to some text editor (e.g. TextEdit or Ms-Word), it pastes the "tag-like text"
- This tag, along with other info, contains the "unique reference number" to the object in its database. This reference number (the actual primary key in the sqlite3 file) is actually the last part of objectID I was talking about above.

*I need to:*
- catch these "tags" in the text file
- exchange them with additional info from the database of that foreign app.

*What I have managed already:*
- My app can find the tags in text
- Extract the reference numbers.

*Now I want to* use these reference numbers to go back to the database and grab the rest of the info to add it to the text document.

Because the foreign app is proprietary, I must use whatever info it provides (the tag), connect directly to its database and get the rest of the info by myself using the tag.
I admit that the picture is not perfect, but that foreign app does not have SDK/API, so I don't see any other solution.

Nov 15, 2010 12:50 PM in response to DavidMan

Question: will the part like <x-coredata://A3EDF6C1-229D-4658-A04E-598ADF1C749A be constant for the particular installation, for every entity/object?

This part is a UUID that is generated when the persistent store is first created and it is stored in a separate table in the DB. It is supposed to be universally unique. The digits at the end after the 'p' are simply a counter that reflects the order in which the 'records' for each entity were stored.

Nov 15, 2010 12:54 PM in response to DavidMan

I get it. You are trying to reverse engineer the internal data store of a 3rd party application so that you can pull data from it.

What is the deal with dragging into a text editor? Why don't you write a little app that knows how to use the pasteboard and take a look at what the application is actually exporting. Perhaps there is better data in there than what you are getting from the text representation.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Core Data: Get Managed Object by unique part of the objectID

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.