How to properly set relationships in Core Data when using setValue and data already exists

Loading...

How to properly set relationships in Core Data when using setValue and data already exists

Let's say I have two objects: Articles and Categories with a many-many relationship between the two. For the sake of this example all relevant categories have already been added to the data store. When looping through data that holds edits for articles, there is category relationship information that needs to be saved.
I was planning on using the -setValue method in the Article class in order to set the relationships like so:
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    if([key isEqualToString:@"categories"]){
        NSLog(@"trying to set categories...");
    }
}

The problem is that value isn't a Category, it is just a string (or array of strings) holding the title of a category. I could certainly do a lookup within this method for each category and assign it, but that seems inefficient when processing a whole bunch of articles at once. Another option is to populate an array of all possible categories and just filter, but my question is where to store that array? Should it be a class method on Article? Is there a way to pass in additional data to the -setValue method? Is there another, better  option for setting the relationship I'm not thinking of?
Thanks for your help.

Solutions/Answers:

Answer 1:

Why are you implementing setValue:forKey:? You should have a relationship between these two objects that is set up as many to many and work with the relationship directly. It appears here that that you are doing this the hard way.

What is your end goal with this code?

Update

If I understand you correctly, you have a blob of JSON data and you want to turn that into an object graph and it matches that object graph correct? If so, please look at the code I wrote in this other answer:

JSON and Core Data on the iPhone

In that answer I show how to recursively construct and deconstruct a NSManagedObject graph to and from a JSON structure. It is quite a bit easier than the path you are going down right now.

Answer 2:

It looks like you have a 1:1 relationship defined, so you are trying to say that the article belongs to one and only one category.

When you populate one object you may need to create the other; so when an article is created you will need to check your core data store for an object of the correct type and if it does not exist you create it. When you have the object, either pre-existing or just created, you either assign (relationship of one) it to the relationship ivar Category* or (relationship of many) add it to the NSSet* expressing the relationship.

So yes you do need to lookup and assign an actual object, since you must create a relationship to an object in the managed object context, and if it doesn’t exist it must be created before that relationship can be made.

References

Loading...

Is there a good UITabBarController Example?

Loading...

Is there a good UITabBarController Example?

Is there a good UITabBarController example where it is NOT created in the appDelegate?
I would like to use a UITabBarController inside of a UIViewController, however dont know how to set the view outlet.

Solutions/Answers:

Answer 1:

This is all very well documented here. It shouldn’t matter where the UITabBarController instance is created, UIApplicationDelegate or not. In a nutshell, Create all of your respective UIViewController’s and add them to an array. Then assign that array to your UITabBarController’s viewControllers property. Then you can simply do something like [window addSubview:myTabBarController.view].

I’m just copy/pasting from the documentation here:

You should never access the tab bar
view of a tab bar controller directly.
To configure the tabs of a tab bar
controller, you assign the view
controllers that provide the root view
for each tab to the viewControllers
property. The order in which you
specify the view controllers
determines the order in which they
appear in the tab bar. When setting
this property, you should also assign
a value to the selectedViewController
property to indicate which view
controller is selected initially. (You
can also select view controllers by
array index using the selectedIndex
property.) When you embed the tab bar
controller’s view (obtained using the
inherited view property) in your
application window, the tab bar
controller automatically selects that
view controller and displays its
contents, resizing them as needed to
fit the tab bar interface.

Answer 2:

Here’s one implementation of UITabBarController initialized in a UIViewController.

The author also posted a github link to the xcode project.

References

Loading...