How to step inside NSManagedObject; access individual attributes - iphone

NSManagedObject *entryObj = [self.fetchedResultsController
objectAtIndexPath:indexPath];
entryObj consists of four String attributes.
If I NSLog entryObj, I get the information I want. I cannot figure out how to access each of these properties individually. I read a similar post where the solution was to call "entity." I cannot figure out how to use "entity" to access a specific attribute.
Any ideas? References? Tutorials?
Thanks in advance.

Properties on managed objects are KVC/KVO compliant so you can access them via:
[entryObj valueForKey:#"name"]
Alternatively you can generate a custom Core Data class with real properties to access these values. See this documentation for more information. The Xcode core data modelling tool can generate these classes for you. While you have the model open, choose "File->New File" and you should see a "Managed Object Class" item. Choose this and select the entities you wish to generate classes for.
Once you have done this and the core data entities have their class name set appropriately, you just cast the NSManagedObject to an instance of your new class and access its properties, i.e.
MyObject *entryObj = (MyObject *) [self.fetchedResultsController
objectAtIndexPath:indexPath];
NSLog(#"Property is %#", entryObj.whatever);

If you build your NSManaged objects with the designer then you can export model classes. From the xcdatamodel do File/New File then pick CocoaTouch Class/Managed Object Class. Next then Next then tick each of your classes. Leave generate accessors and generate obj-c 2.0 properties ticked and click finished.
Now you can include the generated files in your projects and use dot accessor syntax.
Alternatively use [entryObject valueForKey:#"keyname"]; but I prefer to stick to the dot accessor syntax where possible.

For generating real classes with properties to call from your object model, I highly recommend using mogenerator:
http://github.com/rentzsch/mogenerator
That's the main project, but the easy to download installer is here:
http://rentzsch.github.com/mogenerator/
You also get primitive value accessors for numeric types, for free.

Related

Working with Swift and Core Data

This is more of a generalized question as I have yet to write the code for the question I am asking. Before I get started writing the code I wanted to make sure I am on the right track and possibly getting suggestions for better ways to do what I want to do. Basically right now I have a core data model setup in a way that I think is correct for what I am trying to do and just need some guidance on a very specific part of the code but want to make sure overall I created it correctly.
The first part to the question is more of a clarification on how relationships work in core data. Right now I have 5 entities and to make sure I have the correct idea on how it works I will use a few examples to make sure I am on the right track.
So lets save I have an entity I called name. Within that Name entity that contains only a name attribute. Next I have an entity that has classes, that each have a boolean of true or false to determine which class it is. These 2 are related in a inverse relationship of Name entity having a to one relationship and the Classes having a to many relationship because multiple names can have multiple classes but each name can only have 1 class. If I am right on this one that means I full understand core data relationships!
Now the second part of the question is related to the booleans in the class. I have the Class entity which is like I said a boolean containing a true false set as default to false. When the user selects one of the class buttons before presenting the popover where they actually give the name of the class selected it saves the boolean to true then passes that data over to the popover Name view controller. I am very unsure as to how to do this as it isn't a widely asked question on here nor have I been able to find any info through researching. I am one of those people who needs to actually learn by clear examples....any help with this would be appreciated! Sorry I don't have any example code for this.
The first part seems correct. The ManagedObject of your Class CoreDataObject should have an NSSet property which will contain the names (as the Class can have multiple names)
For the second part, Core Data uses objects. When you 'get' the data from Core Data it will be a (probably extended) NSManagedObject (named Class in our case). You can send this object as a parameter just as you would do with any other object and use it as you would use any other object :-). For example looping over de NSSet Names
func iterateOverNames(someClass: Class) {
for name: Name in someClass.names {
// do stuff
}
}
You can check these links for more information:
https://realm.io/news/jesse-squires-core-data-swift/
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/index.html

Eclipse 4 RCP - how to change what is showed in specific area?

I have splitted my application into two main areas.
Part(A)
PartStashContainer(B)
The content of A should be set based on what user wants.
So basically i can have 1..N classes which could be used in Class URI of Part in application model.
I don't know if i should replace the whole Part(A) with new dynamically created Part(C) which has content i want, or i should somehow to modify the existing Part (call setContributionURI, or setObject methods on Part object?).
It does make more sense to me to modify the existing Part, because it is defined in Application model and therefore already describing the location where the content should be.
Possible solutions:
Modify the Part object so it "reload" its content based on new setup (But how? Can setContributionURI or setObject methods help?)
Remove the old Part and add dynamically on same place in Application model the new Part (using EModelService and EPartService).
other solution??
If you want to reuse the Part then do something like:
MPart part = find or inject your part
MyClass myClass = (MyClass)part.getObject();
... call a method of MyClass to change the contents
MyClass is the class you specify for the object in the application model. You should add a method to that to let you change the contents.
Don't try to call setObject, this is really only for use by Eclipse. I don't think setContributionURI would do anything after the part is created (but I am not sure).
If you want to use different classes for the different data then you really should use different Parts.

General Data Modeling overview

I have been really deep thinking about a general way of creating "data model", and been jiggling with best practices and MVC pattern. Currently I am using a singleton pattern to get my httprequest and json parser (which comes as NSDictionary). Now rather than accessing this parser directly, I was hoping to make a Data model that can be binded through this.
However, I have been struggling if there is an easy way to do that rather than assigning manually "[myObj setValue:[jsonDict objectForKey:#"name"]];" where myObj tends to be a simple NSString object.
Since NSDictionary is a nice KVC concept, how can I utilize this to enrich a better style of data model in which I can generally access myObj.name or myObj.address entity than "[myObj setValue:[jsonDict objectForKey:#"name"]];" behavior.
I have looked into "Core Data" model, however the current design doesn't require to store anything locally, but just within memory for security reasons.
Any good ideas or best practices solution here will be really helpful.
Just create your classes. Then crate an class that will serialize the data from your dictionaries to your object.
Let say you create an class Person that has properties firstName and lastName. Then you crate a Class like PresonController, that will do manage the person objects, and in it create class methods like
+(Preson *)personFromDictionary:(NSDictionary)peseonDictionary;
And every time you need to create an person from an dictionary you will do
Person *newPerson = [PersonController personFromDictionary:yourPersonDictionary];
And then in the code you just access the properties of the Person object
NSLog(#"Person first name:%#",newPerson.firstName);
Hope I was clear enough for you.

Entity Framework 4.0: Create an unmapped property in the model (currenty i get : error 11009 - Property is not mapped)?

I know that i can enter/add new properties via code manually into partial classes but i wanted to use the model to add my new properties - reason being is that i can control a number of different attributes like NULL and things like that... and of course the code generations works great..
I added some foreign keys manually just on the model and they work great.
But everytime i add a SCALER PROPERTY i get an error in vs 2010 which says
Error 2538 Error 11009: Property 'testprop' is not mapped.
I can't believe i must map a custom property that i created to a column in the db.... is there no way to say "IGNORE" this property or treat as an unmapped property??
This way my code generation will create the required items BUT i don't get the error
Any help on this would be really helpful.
As i say i know i can edit things manually but wanted to update the model rather than edit a partial class....
I am sure i am missing something obvious?
With EntityFramework 5 you can use the NotMappedAttribute for unmapped properties. So, you can migrate to EF5 or use partial classes on EF4.
I believe that EF will on allow you to use the Model Designer to map to something that exists. If you want to create a property that doesnt exist, you'll have to use the partial class.
I had the same error - you can use the NotMappedAttribute for unmapped properties...

Where should I keep Core Data fetches and convenience methods?

I've got a number of convenience methods that perform fetches for my Core Data entities. For example, a method that returns currently-active activities, or a time interval between two completed activities, or a default client if there is one. I also have convenience methods to initialize and add entities with various attributes.
I have a data model singleton class that is the go-to class for initializing Core Data and getting the NSManagedObjectContext, etc.
Is it better to put these convenience methods in the data model singleton class, or in each relevant entity subclass as class methods? I don't think there's a One True Way here, but I would like opinions and experiences. Thanks!
I would associate them with the class on which they operate. To do this, I would first generate class files for your entities (select the entities in the editor, then File > New File > NSManagedObject).
Then, just put the methods in the class files, such as:
+ [Activity activeActivities];
- [Activity intervalToActivity:(Activity *)other];
+ [Activity activityWithVariousAttributes]; // (plus maybe a corresponding initWithVariousAttributes)
The general rule I'd give is that if the method operates on a specific class, then put the method in that class. =)
To expand on what Dave said, you could add your convenience methods to a category (e.g. FooManagedObject+Convenience.h/.m) so that when you change your data model and regenerate your NSManagedObject subclasses (i.e. FooManagedObject.h/.m), you don't end up clobbering your convenience methods.
Another option is using mogenerator which instead of categories, maintains a private (auto-generated) NSManagedObject subclass and a public subclass of the auto-generated subclass that you can add your own methods to. This way when you change the data model, only the private subclass is regenerated, but the subclass where your convenience methods live is left untouched. It's more work to setup compared to simply using categories, but it adds a few additional convenience methods and is well worth the effort to setup.