ios - How to get Core Data object from specific Object ID? -
i can object's id in core data using following code:
nsmanagedobjectid *moid = [managedobject objectid];
however, there way object out of core data store giving specific object id? know can using nsfetchrequest, this:
nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; nsentitydescription *entity = [nsentitydescription entityforname:@"document" inmanagedobjectcontext:managedobjectcontext]; [fetchrequest setentity:entity]; nspredicate *predicate = [nspredicate predicatewithformat:@"(objectid = %@)", myobjectid]; [fetchrequest setpredicate:predicate];
however, i'd in way not initiate own fetch request. ideas?
you want:
-(nsmanagedobject *)existingobjectwithid:(nsmanagedobjectid *)objectid error:(nserror **)error
fetches object store has id, or nil if doesn't exist.
(be aware: there 2 methods on nsmanagedobjectcontext similar-seeming names tripped me up. keep them straight, here's other 2 do:
-(nsmanagedobject *)objectwithid:(nsmanagedobjectid *)objectid
...will create fault object provided objectid, whether or not such object exists in store. if doesn't exist, fires fault fail unless insert object first nsmanagedobjectcontext's insertobject:
. use i've found copying objects store store while preserving objectids.
-(nsmanagedobject *)objectregisteredforid:(nsmanagedobjectid *)objectid
...will return object has id, if has been fetched store managedobjectcontext. if knows method useful for, please comment.)
[eta.: important difference between first method , other 2 existingobjectwithid:error:
never returns fault; fetches whole object you. if you're trying avoid (e.g. working expensive-to-fetch object big blob property), have clever objectwithid:
or objectregisteredforid:
, don't fire faults; or use configured fetch request.]
Comments
Post a Comment