我想要的是:
1)创建一个新的手动对象
获取它的临时ID与 [myMO objectID];
3)将该ID转换为NSURL,保存它以供将来参考:
NSManagedObjectID * moID = [myMO objectID]; NSURL * url = [moID URIRepresentation];4)保存托管对象上下文
5)稍后,使用NSURL作为ID获取该对象
NSManagedObjectID * moID = [[context persistentStoreCoordinator] managedObjectIDForURIRepresentation: url];猜测:它不工作。我从
得到一个空蠢的对象 NSManagedObject * myOldMo = [context existingObjectWithID:moID error:& error]但是...正如我所说的...创建托管对象时,ID是临时的。所以它确实有道理为什么这不工作。我必须首先保存上下文,然后我得到一个持久性ID。真正的一个。
1)创建托管对象
2)保存上下文
3)以NSURL形式获取ID
4)任何时候,例如在您的下一个生日,使用NSURL访问managed对象; - )
我试图梦想的NSManagedObjectID像一个DB id,我可以写在一些黄色的postIt表和胶水在我的显示器的中间,所以我参考它午饭后。你知道...至少在过去,我们通过telnet和执行的SQL命令手动地使用数据库来查询订单信息和类似东西。
但是Core Data有这个有点奇怪的NSManagedObjectID的东西。
你的秘密策略是什么?你真的认识到许多用例,你会需要NSManagedObjectID?
解决方案我不知道这是一个很大的秘密。该文档描述了从 NSManagedObjectContext 中获取托管对象的永久ID的方法:
- (BOOL)getsPermanentIDsForObjects:(NSArray *)对象错误:(NSError **)错误developer.apple/iphone/library/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.html#//apple_ref/occ/instm/NSManagedObjectContext/obtainPermanentIDsForObjects:error :
What I'm trying is this:
1) Create a new manged object
2) Get it's temporary id with [myMO objectID];
3) Convert that ID to an NSURL, so I can save it for future reference:
NSManagedObjectID *moID = [myMO objectID]; NSURL *url = [moID URIRepresentation];4) Save the managed object context
5) Some time later, fetch that object using the NSURL as ID
NSManagedObjectID *moID = [[context persistentStoreCoordinator] managedObjectIDForURIRepresentation:url];And guess what: It does not work. I get an empty-stupid object back from
NSManagedObject *myOldMo = [context existingObjectWithID: moID error:&error];But...as I said...the ID is temporary when creating an managed object. So it does make sense why this doesn't work at all. I must first save the context, and then I get a persistet ID. The real one. Right?
So is that the way to go?
1) Create the managed object
2) Save the context
3) Get the ID as NSURL
4) any time later, for example on your next birthday, access the managed object with the NSURL ;-)
I try to dream of NSManagedObjectID like a DB id which I can write on some yellow postIt sheet and glue on the middle of my monitor, so I refer back to it after lunch. You know... at least like in the old days where we used databases over telnet and executed SQL commands manually to query order information and stuff like that. The ID was the most important and significant thing, all the time.
But Core Data has this somewhat strange NSManagedObjectID thing.
What are your secret strategies? Do you actually recognize many use cases where you would need that NSManagedObjectID? Or is that something I could easily forget with no pain afterwards?
解决方案I'm not sure that it's such a big secret. The documentation describes the way to get permanent IDs for managed objects from the NSManagedObjectContext:
- (BOOL)obtainPermanentIDsForObjects:(NSArray *)objects error:(NSError **)errordeveloper.apple/iphone/library/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.html#//apple_ref/occ/instm/NSManagedObjectContext/obtainPermanentIDsForObjects:error:
更多推荐
如何使用ManagedObjectID正确的方式?
发布评论