NSManagedObject subclass properties - ios

I had a subclass of NSObject that acted as my app's main data model, with lots of properties and methods. I decided I wanted to use Core Data to saved this data, so I changed the subclass to be of NSManagedObject. I created the entity in the .xcdatamodeld and linked it with my NSManagedObject subclass. What I'm wondering is if it is okay to keep properties in my subclass that are not saved? For example, this class contains a NSOperationQueue property, but of course I don't want Core Data interacting with this property.

When you automagically create (or re-create) an NSManagedObject from an Entity you lose anything else you had in that file. It is common for people to use categories to customize the logic for NSManagedObjects. It allows you to add customization to the default NSManagedObject without changing the code in that file!
Nice and clean.
This link may be helpful: http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html

Yes, it is perfectly alright to have properties and methods of its own in a NSManagedObject subclass.
Additionally, if you want any of those properties to take advantage of Core Data caching mechanism, you can add them as transient to your entity model.

Related

Create NSManagedObject Subclass from Core Data Data Model in Xcode 6

I've got two entities in a Core Data Data Model. There is a one to many relationship between them. When I navigate to Editor --> Create NSManagedObject Subclass..., my models are generated but I do not get strongly typed method signatures like - (void)addPhotosObject:(NSManagedObject *)value; on the many side of the relationship.
According to what I've read, in Xcode 5 at least, this is because the class on the one side of the relationship was created first so the class on the many side of the relationship does not yet exist. The suggestions I see say to to simply Create NSManagedObject Subclass... again and the method should generate methods with strongly typed classes but the method signatures are not changing to strongly type.
Is there a trick to getting strongly typed method signatures?
After deleting the NSManagedObject subclasses that were initially created by Create NSManagedObject Subclass... and simply re-generating them by selecting Create NSManagedObject Subclass... again, the method signatures ended up strongly typed which gave me a Photo as a parameter like this - (void)addPhotosObject:(Photo *)value; instead of an NSManagedObject.

How to use NSManaged object class using NSObject class

I have NSObject class name TrackInfo which contains tracks info like name , artist name,thumb image etc.
I use this class as downloading data and save information to that class after parsing data.
Now I have another tab in which, I have to show some data. This is same kind of data like trackInfo. But when app is in OFFLINE, I have to make NSManagedObject. It is same as trackinfo.
Can I use NSObject class instead of NSManagedObject or Vice-Versa ?
What I basically wants to do is, I have to display track info from one class either Trackinfo (NSObject class) or NSManagedObjectClass which is used to save data when app is in offline.
Short answer is yes, you can. How? You can find a useful discussion Organising Core Data for iOS.
The long answer can be grabbed within the documentation.
NSManagedObject is a generic class that implements all the basic
behavior required of a Core Data model object. It is not possible to
use instances of direct subclasses of NSObject (or any other class not
inheriting from NSManagedObject) with a managed object context. You
may create custom subclasses of NSManagedObject, although this is not
always required. If no custom logic is needed, a complete object graph
can be formed with NSManagedObject instances.
A managed object is associated with an entity description (an instance
of NSEntityDescription) that provides metadata about the object
(including the name of the entity that the object represents and the
names of its attributes and relationships) and with a managed object
context that tracks changes to the object graph. It is important that
a managed object is properly configured for use with Core Data. If you
instantiate a managed object directly, you must call the designated
initializer (initWithEntity:insertIntoManagedObjectContext:).
About your question, it depends on what you need to achieve. If your goal is to perform a sync mechanism between your device and the server, you should set up 1) a model with a TrackInfo entity 2) a Core Data stack that relies on a persistent store like SQLite. Then you should modify TrackInfo to take into account modifications to that entity. For example, a dirty flag property (0 or 1) or a timestamp. When you do a modification on your TrackInfo you update that property. When the connection is restored you need to query against that property and sync with the server. If you choose the timestamp, the server should say what is the latest timestamp to query against.

Connect custom class with CoreData

Is it possible to add custom class(not inherited from NSManagedObject) as Entity in CodeData?
Or that is the best way to do if I have Custom class(user profile).
I want to save some users in Core data. Problem is that my Custom class will be changed in future. So, I want that Custom class and CoreData Entity will be inherit from one class. Is it possible to do?
All entities in Core Data are instances of, or instances of subclasses of, NSManagedObject. You have no choice about this.
If you only want to save some user profiles then you should maintain a subclass of NSObject which you usually use and which is able to be created from an NSManagedObject instance (that you have fetched from the data store for that purpose). You will also need a way to create / update an NSManagedObject instance with new details from this user profile class.
If you change the user profile class but not the attributes of the entities in the Core Data model then you don't need to do a migration of the data store.

Discrepancies Between Core Data Editor and NSManagedObjectSublcass

How are discrepancies between the Core Data Editor and the custom NSManagedObject subclass handled by Xcode?
For instance, let's say in the editor I have and Entity called Person with attributes firstName and age. I then create an NSManagedObject subclass of Person from the editor and in the header of the subclass I add the attribute lastName but I don't update the editor with this new attribute.
Depends somewhat on exactly how you define the property, but usually it will be treated like a transient property, so its value will not be set when the object is retrieved from the store unless you write some custom code to set it, nor will Core Data make any attempt to save it to the store. Also you won't be able to use the property in any fetch or sort predicate that results in core data generating sqlite SQL calls.

Save an existing Model to a CoreData DB

I'm learning Core Data and I understand all the examples for creating a brand-new object, assigning values and saving it to the managedContext (insertNewObjectForEntityForName).
However, what if I've already created an object elsewhere (model Category)? In this case I'd want to just assign the current Context to this Model, and then save it.
What is the command/approach to take an in-memory Model, and then assign to a context so it can be saved?
If you want to use Core Data to manage your data, you'll need to:
create an appropriate model description (.xcdatamodeld file)
modify your model class(es) so that they inherit from NSManagedObject
set the "Class" for each entity in your model description to one of your NSManagedObject subclasses
add code to your app to create and manage the Core Data stack, fetch data, etc.
This is all very do-able, but I wouldn't recommend that you attempt it until you have a solid understanding of Core Data and your reasons for adopting it in your project. The lack of clarity in your question may indicate that you're not quite there yet; you might benefit from working on a small project that uses Core Data from the start.
If all you want to do is to save your data, you should know that Core Data is not the only way to do that. A much simpler approach to saving your data would be to adopt the NSCoding protocol in your data model and then use a NSKeyedArchiver to store your data. Get the full story from the Archives and Serializations Programming Guide. There are other ways to do it as well, but NSKeyedArchiver is a good place to start.
You can only save NSManagedObjects (and their subclasses) to CoreData. NSManagesObject can not be created except in the context of an NSManagedObjectContext.
So, what you're saying is confusing. Do you have a non-CoreData model object?

Resources