How do you map a property that is a collection of strings using RestKit - restkit

I am currently using RestKit 0.20.1 to map JSON coming back from my server. I am able to map any single property and any collection of non-primitive values just fine, but I can't figure out how to map a collection of primitive properties, such as a collection of strings or integers.
For example, I am retrieving the following:
{
"id":3,
"name":"john",
"favorite_numbers":[1, 2, 3, 4, 5]
}
And I want to map it to an entity that looks like:
//MyEntity
#property (nonatomic, strong) NSNumber *identifier;
#property (nonatomic, strong) NSString *name;
#property (nonatomic, strong) NSArray *favoriteNumbers; // This is a collection of numbers
How can I define a property mapping for the favoriteNumbers property?

Just map favorite_numbers to favoriteNumbers. Because favoriteNumbers is defined as an NSArray the values should effectively be copied directly by the mapping.

Related

Using "set" in cocoa method name

In my class, I have:
#property (atomic, retain) NSPredicate* predicate;
So obviously setPredicate:somePredicate works.
I want a class method that looks like:
-(void)setPredicateWithDate:(NSDate *)date
{
NSPredicate* predicate = (build a predicate with the NSDate)
[self setPredicate:predicate];
}
This seems to conflict with Cocoa's naming conventions because I do not have a property called predicateWithDate. What do folks usually name these sort of methods?
I was thinking "applyPredicateWithDate:"
Thoughts?
Naming a method that starts with set is absolutely fine. Not all methods that start with set has to be a setter directly to the instance variable named after set, it should just do what it says and change the ivars in your class accordingly, in which your case is perfectly fine.

How can I store array that contains PFObjects in NSUserDefaults?

Some PFObject contains PFFile(Image file) and some are not.When I am trying to store custom object in to user default using NSKeyedArchiver and NSKeyedUnarchiver, it won't work.
Help me how to store PFObjects array into UserDefault which contains PFFile type field.
From NSUserDefaults documentation:
A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.
So you can get your file's data by getDataInBackgroundWithBlock and save it to NSUserDefaults:
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
[[NSUserDefaults standardUserDefaults] setObject:data forKey:#"file"];
[[NSUserDefaults standardUserDefaults] synchronize];
}];
There is also a library called Parse-NSCoding which adds NSCoding extension to Parse objects. It is not updated but you may check it out.

moveRowAtIndexPath and CoreData Relationships

I'm stuck at ordering Core Data relationships. I have a to-many relationship but the problem I can't overcome is that re-ordering relational entities in one view re-orders them in every view they belong to.
Example: People are added to activity1 and are randomly ordered using the moveRowAtIndexPath method. Those same people get added to activity2 and are ordered identically to activity1. How can I stop this?
Relevant code:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSUInteger fromRow = [fromIndexPath row];
NSUInteger toRow = [toIndexPath row];
id object = [[eventAthletes objectAtIndex:fromRow] retain];
[eventAthletes removeObjectAtIndex:fromRow];
[eventAthletes insertObject:object atIndex:toRow];
int i = 0;
for (object in eventAthletes)
{
[object setValue:[NSNumber numberWithInt:i++] forKey:#"athleteDisplayOrder"];
}
[self saveContext];
[object release];
}
I updated the model and made the attribute athleteDisplayOrder transient thinking that by using the key and NOT saving it, I would be able to start fresh in a new event. No Dice.
The identical ordering is expected behavior. If you want to "randomize" the order, you have to explicitly do that by reassigning random athleteDisplayOrderattributes.
EDIT:
If you want to have a special user definable order of athletes for each event one way to solve this is to create a new entity EventAthletes that has to-one relationships both Event and Athlete and also contains a display order attribute.
END EDIT
BTW, you are using an extra data array with core data for display in a table view. Did you know that you can use NSFetchedResultsController to manage your table view datasource much more efficiently?

Saving changes to a NSManagedObject

I have a problem with making changes to ManagedObjects and saving those changes to the persistent store.
What does work is deleting objects, inserting object. and fetching those objects. As said, i fail making a change to a fetched managed object.
I have two view controllers.
Both have a public property:
#property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
The AppDelegate.m file sets this property for the first view controller in the application:didFinishLaunchingWithOptions: method like so:
// get reference to view controller //
//..
controller1.managedObjectContext = self.managedObjectContext;
view controller1 again passes the managedObjectContext when pushing to the second view controller, it does this in the prepareForSegue: method like so:
// Get reference to the detail view //
MySecondViewController *controller2 = segue.destinationViewController;
// Pass the managed object context //
controller2.managedObjectContext = self.managedObjectContext;
Within this second view controller I fetch objects from core data and I store them in a property of the view controller like this:
#property (nonatomic, strong) MyKindOfObject *object;
...
self.object = [fetchResults lastObject];
Fetching objects seems to work fine as i nicely get results..
The fetched object has a number of properties, one of them is of type NSString. The value of this property I show in a UITextField. The user can change the value here and when done he presses a button. I catch the action and I do the following:
self.object.mytext = textField.text;
followed by trying to save to core data:
// Save to CoreData //
NSError *error = nil;
if(![self.managedObjectContext save:&error]){
// handle the error //
NSLog(#"is error");
}
else{
NSLog(#"no error");
}
1) The next time the user returns to this view controller, again the object will be fetched. But it still has the old value.
2) Also I use a Firefox add-on called SQLite Manager to keep an eye on the data within the related sqlite file. When stepping through the code, after calling the save method nothing changes in the file.
3) Also within Xcode i use the Variables View to keep an eye on the self.managedObjectContext object. When I am storing the new data into my object, right before calling save, none of the properties of self.managedObjectContext change (like unprocessedChanges or changedObjects).
4) I also added a call to the hasChanges method of the managedObjectContext, but this returns NO:
if([self.managedObjectContext hasChanges]){
NSLog(#"changes managed object context!");
}
5) I also added a call to the hasChanges method of the related managedObject, but this also returns NO:
if([self.object hasChanges]){
NSLog(#"changes in managed object!");
}
I am probably doing something totally wrong here but I can not figure out what it is.
I truly hope somebody can help me out?.
Thanks in advance!
Oke let me answer my own question: as the newby that I am, i indeed did something totally wrong. I was thinking I knew better then apple themselves. lesson learned: I do not!
After creating entities within the model I created custom managed object classes for each entity (or actually NSManagedObject subclasses for each entity).
Within the generated implementation files all properties (attributes) are implemented as #dynamic. Due to lack of knowledge I did not understand/recognise this. So the newby in me thought: lets change all that to #synthesize.
Hopefully this post can help some other newby to not make the same mistake.

NSManagedObject get parent object

I have an NSManagedObject who has a one to many relationship with the parent Object. How can I get the parent object using the child object?
You can set up a #property to access it and then call the #property
id parent = [myChild parent];
Or you can access it via KVC
id parent = [myChild valueForKey:#"parent"];

Resources