Store CLRegion attribute in Core Data - ios

I need to add a CLRegion to my Core Data Entity but I don't what type to select for it.
I read this doc but I'm still confused on how to get it setup correctly. If someone could provide an explanation I'd reall appreciate it.

For the entity attributes type for the CLRegion select "Transformable" Then define what the object is in the the NSManagedObject that Core Data generates for you. (see location below)
#interface Person : NSManagedObject
{
}
#property (nonatomic, retain) NSString * firstName;
#property (nonatomic, retain) NSString * lastName;
#property (nonatomic, retain) CLRegion * location;
#end
#implementation Person
#dynamic firstName;
#dynamic lastName;
#dynamic location;
#end

Related

How to store coredata entity object into NSManageobject Class in core data?

I have an class of core data entity like this,
#interface Answer : SMManagedObject
#property (nonatomic, strong) NSDate *expiresAt;
#property (nonatomic, retain) StudentAssessments * assessment;
#property (nonatomic, retain) Question * question;
#property (nonatomic, retain) Assignment * assignment;
#end
I want to store object of StudentAssessments into Answer object class. So how can i do this?
Its raising error where i do that code for storing like,
[newObjOfAnswer setAssessment:(StudentAssessments *)self.contentObject];
Here self.contentobject is as StudentAsssessments class.
Your help will be appreciated.

Having trouble with new field in entity (unrecognized selector error)

I'm trying to add a field to an entity and whenever I try to access the new field I get
unrecognized selector sent to instance...
Everything else about the entity works. I'm able to add objects and I'm able to assign values to other fields in the object, but not to the new objects.
Of note, I also deleted the DerivedData directory and I deleted the .mom/.momd files to make sure the tables are being built correctly.
Any thoughts?
Locations.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#class Games, Players;
#interface Locations : NSManagedObject
#property (nonatomic, retain) NSNumber * defaultLoc;
#property (nonatomic, retain) NSString * locationAddr1;
#property (nonatomic, retain) NSString * locationAddr2;
#property (nonatomic, retain) NSString * locationCity;
#property (nonatomic, retain) NSNumber * locationID;
#property (nonatomic, retain) NSString * locationName;
#property (nonatomic, retain) NSString * locationState;
#property (nonatomic, retain) NSString * locationZip;
#property (nonatomic, retain) NSNumber * numberOfCourts;
#property (nonatomic, retain) NSNumber * defLoc;
#property (nonatomic, retain) NSSet *haveGames;
#property (nonatomic, retain) NSSet *havePlayers;
#end
#interface Locations (CoreDataGeneratedAccessors)
- (void)addHaveGamesObject:(Games *)value;
- (void)removeHaveGamesObject:(Games *)value;
- (void)addHaveGames:(NSSet *)values;
- (void)removeHaveGames:(NSSet *)values;
- (void)addHavePlayersObject:(Players *)value;
- (void)removeHavePlayersObject:(Players *)value;
- (void)addHavePlayers:(NSSet *)values;
- (void)removeHavePlayers:(NSSet *)values;
#end
Locations.m
#import "Locations.h"
#import "Games.h"
#import "Players.h"
#implementation Locations
#dynamic defaultLoc;
#dynamic locationAddr1;
#dynamic locationAddr2;
#dynamic locationCity;
#dynamic locationID;
#dynamic locationName;
#dynamic locationState;
#dynamic locationZip;
#dynamic numberOfCourts;
#dynamic defLoc;
#dynamic haveGames;
#dynamic havePlayers;
#end
Note: defLoc and defaultLoc are two fields that I added. These are the ones giving me problems.
Executing code:
- (IBAction)updateLocation:(UIStoryboardSegue *)segue
{
// If it is not "Edit" it is an "Add" and we need to insert a newobject.
if ([segueType1 isEqualToString:#"Add"])
{
NSManagedObjectContext *context = [[self fetchedResultsController] managedObjectContext];
location = [NSEntityDescription insertNewObjectForEntityForName:#"Locations" inManagedObjectContext:context];
}
location.locationName = lName;
location.locationAddr1 = lAddr1;
location.locationAddr2 = lAddr2;
location.locationCity = lCity;
location.locationState = lState;
location.locationZip = lZip;
location.numberOfCourts = lNumCourts;
location.defLoc = lNumCourts;
// location.defaultLoc = [NSNumber numberWithInt:1];
// DLog(#"ldefaultLocation = %#",ldefaultLocation);
location.defaultLoc = ldefaultLocation;
segueType1 = #"Add"; // Always reset back to Add so that segues work right
}
location.defLoc above is the command that gives the error...All other location statements work fine.
That can happen when you don't declare the new attribute in your NSManagedObject child. As soon as you add a new field in the core data model, remember to add the right property to the NSManagedObject class and declare it #dynamic in the implementation.

Use transient property on any Core Data attribute that doesn't need to be saved?

I'm trying to understand transient properties. I have this object:
#interface AddressAnnotation : NSObject <MKAnnotation>
#property (nonatomic, copy) NSString *address;
#property (nonatomic, copy) NSString *city;
#property (nonatomic, strong) NSNumber *latitude;
#property (nonatomic, strong) NSNumber *longitude;
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, strong) NSString *state;
#property (nonatomic, strong) NSString *street;
#property (nonatomic, strong) NSString *zip;
#property (nonatomic, strong) NSString *name;
I use this to show my annotation on a MKMapView. I want to save these pins in some Route entity. A Route would just let the user name the route. For my app, the only thing really important is the latitude and longitude. The other properties I can always recalculate with a reverse geocoder since I have the lat/long. To save space, I was thinking that if I want to make this object a Core Data entity, can I make all the properties that are not latitude and longitude transient properties? I read some examples where transient was used for a prperty that was calculated based on other non-transient properties. Is this a proper use of transient? Thanks.
That would be the case you can apply transient property in your example. From my perspective, you can still keep some properties in the core data to avoid redundant query for later use. For example, the address you can use reverse geocode to get the real address by longitude and latitude. But you will let user to wait for query address every time when you use reverse geocoder. I will prefer to keep the properties in the core data if that property needs a bit of calculation or wait for connection.

RestKit CoreData one to many relationship mapping, the To Many part not working

I am using RestKit 0.20 to map 2 entities.There is a one to many relationship.
Teacher<->>SchoolClass
Here is the Teacher.h
#class SchoolClass;
#interface Teacher : NSManagedObject
#property (nonatomic, retain) NSString * firstName;
#property (nonatomic, retain) NSString * lastName;
#property (nonatomic, retain) NSNumber * teacherId;
#property (nonatomic, retain) NSSet *teachesClass;
#end
#interface Teacher (CoreDataGeneratedAccessors)
- (void)addTeachesClassObject:(SchoolClass *)value;
- (void)removeTeachesClassObject:(SchoolClass *)value;
- (void)addTeachesClass:(NSSet *)values;
- (void)removeTeachesClass:(NSSet *)values;
#end
Here is the SchoolClass.h
#interface SchoolClass : NSManagedObject
#property (nonatomic, retain) NSString * classCodeId;
#property (nonatomic, retain) NSString * classDesc;
#property (nonatomic, retain) NSString * classRoom;
#property (nonatomic, retain) Teacher *classTeacher;
#end
The code for the relationship mapping is:
[classMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"teacher" toKeyPath:#"classTeacher" withMapping:teacherMapping]];
The results are that in the SchoolClass objects, the classTeacher properties are correctly added. However in the Teacher objects, the teachesClass properties are all empty. Is this expected behavior or I missed something?
Thanks
Ray
Somehow the problem is gone now. All the relationships now working fine. Not sure exactly what happened. Perhaps because I did a reset for the simulator after made a json change. Previously the json results had a problem that caused both side of the relationship problems. After it was fixed, the SchoolClass objects were fine but Teacher objects had relation problems. Now both are fine.

Setter for NSManagedObject creates _CDSnapshot_Provence_

I have two NSManagedObject:
DataEntered
Provence
There is a relation between them: DataEntered must have ONE Provence, and Provence may have one/multiple DataEntered.
All is working well, but when using Instruments and Allocations, every time I set the Provence to DataEntered, a new _CDSnapshot_Provence_ appears in #Living:
Provence * provence = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[self.dataEntered setAddress_provence:provence];
The setter for Provence in DataEntered is managed by CoreData, there is no customization.
When I save DataEntered, is saved correctly. What can cause the creation of multiple living _CDSnapshot_Provence_ ?
Thanks!
#class Provence;
#interface DataEntered : NSManagedObject
#property (nonatomic, retain) NSString * name;
#property (nonatomic, strong) Provence *address_provence;
#end
#class Provence;
#interface DataEntered : NSManagedObject
#property (nonatomic, retain) NSString * name;
#property (nonatomic, strong) Provence *address_provence;
#end
#class DataEntered;
#interface Provence : NSManagedObject
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSSet *dataEnteredAddress_Provence;
#end
#interface Provence (CoreDataGeneratedAccessors)
- (void)addDataEnteredAddress_ProvenceObject:(DataEntered *)value;
- (void)removeDataEnteredAddress_ProvenceObject:(DataEntered *)value;
- (void)addDataEnteredAddress_Provence:(NSSet *)values;
- (void)removeDataEnteredAddress_Provence:(NSSet *)values;
#end
#import "Provence.h"
#import "DataEntered.h"
#implementation Provence
#dynamic name;
#dynamic dataEnteredAddress_Provence;
#end
I saw exactly the same thing and I believe that this is to be expected.
See the section Conflict Detection and Optimistic Locking in the Apple docs at
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdChangeManagement.html
"When Core Data fetches an object from a persistent store, it takes a snapshot of its state. A snapshot is a dictionary of an object’s persistent properties—typically all its attributes and the global IDs of any objects to which it has a to-one relationship."
There is also a section at that same link that is useful to read - Snapshot Management
The problem I ran into was getting Core Data to release its memory allocations after I had faulted all the managed objects or did a context reset.
I just published a blog posting on this and related topics:
Core Data issues with memory allocation - http://finalize.com/2013/01/04/core-data-issues-with-memory-allocation/
Hope this helps.
Scott

Resources