I have a custom class called Person that I want to store in UILocalNotification's userInfo (NSDictionary).
Is there any way to save it without converting it to NSData object using -encodeWithCoder:?
One simple workaround is to give every person a unique id and store the id in userInfo.
Save an instance of person and fetch it with the id whenever you need to get the userInfo
The userInfo dictionary has some limitations exposed here.
So I think, you need to serialize the Person object to be able to store it in that location.
Related
I would like to store data in the contact store that is part of CNContact. Is there a property of NSObject or CNContact that I can store the contents of a Data structure or NSDate object? I would like to be able to keep up with when a CNContact was last modified. I haven't found anything way that Apple has given us to specifically do this. I don't want to save the date of modification in UserDefaults or CloudKit or Core Data or any other way of persisting data. I don't want to use the Note property of CNContact, since it would be able to be changed by the user. The dates instance property of CNContact is get only, and I haven't found any way of using that property to do what I want to do.
An alternative would be to compare hash values or to us the .isEqual method of CNContact or use the === or == operators. Would that work?
Given a group of core data entities (let's call them phrases), what is the best/most correct way of having one phrase as "the active phrase" and enforcing only one active phrase at a given time.
I thought about having a Bool value on the phrase entity, but I don't know how to enforce true to only be on one entity out of the set. I guess I could also have another entity (activePhrase) which stores just one record in it but again I'm not sure how to enforce the 'select one of many phrases to be active' requirement.
I fear this may be a dumb question - my CoreData (and indeed database) knowledge is a bit rusty and I want to make sure I'm following best steps.
I'd get the active object's object ID and save that in user defaults.
You'd get something you can save by:
Asking the managed object for its objectID
Asking the object ID for its URIRepresentation (an NSURL)
Converting the NSURL to NSData by using NSKeyedArchiver's archivedDataWithRootObject method.
Then save that to user defaults. To reverse the process,
Read the NSData object from user defaults
Convert that back to an NSURL by using NSKeyedUnarchiver's unarchiveObjectWithData method
Convert the NSURL back to an NSManagedObjectID by using NSPersistentStoreCoordinator's managedObjectIDForURIRepresentation method.
Get the actual managed object by using NSManagedObjectContext's existingObjectWithID method.
I am using Entity's Object ID in order to uniquely identify local notifications and modify them. I observed that first time when I save my entity, it has following object ID:
<x-coredata:///Task/tE1C5A230-A419-42D5-AF78-3327A09D13BD2>
If I don't exit my application, and try to modify notification, object ID doesn't change and I can modify my notification.
Now, if I restart my app and try to access that entity again, it has different object ID:
<x-coredata://D6703834-ECB4-487B-84F8-330A215E16B7/Task/p13>
So I can't modify notification, as object ID for entity is different. Interesting thing is whenever I access that entity, Object ID remains same as the last one.
So my Question here is why Core data shows different object ID for the first time entity is created? When I try to access entity after opening app again for many times, the object ID (different than the first one) remains constant. I am curious to know why is it happening so?
Please note:
I know there are many posts on SO pointing out that using Object ID is not a reliable approach. Still I want to know reason that why two IDs are being shown.
the first OID is a temporary OID - a temporary id denotes objects that have not been saved yet. the 2nd id is a permanent one and is assigned to a MO AFTER it has been saved:
so...
var objectID = object.objectID
if objectID.temporaryID {
object.managedObjectContext.save() //try do catch left out
}
objectID = object.objectID
assert(objectID.temporaryID == false)
When I GET the server, I check the attribute "objectID" on each object, and if the objectID is already in the local store, I don't store the object.
Question: What's the approach if the "objectID" exists, but its other attributes have changed and thus the entire object should be replaced with the new object? There is an other attribute called "lastModified" that will change. Do I compare against both "objectID" and "lastModified"?
Generally, yes, compare the id and then check the modified date. Try not to replace the object though, instead, pass the new object to the existing object and have it update itself (then throw away the new object).
Note that if you were using Core Data then RestKit could handle this for you using unique identifier (so it can find the existing objects and update them during the mapping process).
I would like to add a NSDictionary into a NSManagedObject Category class (or the NSManagedObject class itself)
When I do this, and I try to access the property, an exception is thrown.
Is this actually possible? I can't add this property as transient in the model because there is no NSDictionary Data Type, of course.
Thanks!
You don't say how you have currently created the property or what the exception is, but from the description you give it sounds like you should be setting the attribute in the Core Data model to be transformable. Setting it to be transformable will cause the NSDictionary to be archived (and unarchived) as you use it using the standard NSCoding protocol. Be sure that everything you put into the dictionary supports the NSCoding protocol so that it is properly archived and restored.
Using transformable is the way. Below are few more insights on the transformable property.
The Transformable data type is a special data type that allows us to
create attributes based on an Objective-C class (custom objects). This
data type is heavily used for storing instances of UIImage, UIColor,
and so on. As the information stored in the persistent store has to be
in the form of NSData instance, while using Transformable data type,
we need to create Value Transformers to convert the custom object
(information in attribute of Transformable data type) into an instance
of NSData (before storing in the persistent store) and to convert the
instance of NSData back to custom object while retrieving from the
persistent store.