Reputation: 750
I have two core data models with int64_t properties. One of them works fine while the other throws EXC_BAD_ACCESS when I try to assign a non-zero value to the integer field. I've read the answers that say to recreate the NSManagedObject child class and I have done with no success. The broken class looks like this:
@interface NoteObject : NSManagedObject
@property (nonatomic) int64_t remoteID;
@property (nonatomic) int64_t remoteArticleID;
@property (strong, nonatomic) ArticleObject *article;
@property (strong, nonatomic) NSString *status;
@property (strong, nonatomic) NSString *token;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *noteContent;
@property (strong, nonatomic) NSDate *pubDate;
@property (strong, nonatomic) NSDate *modDate;
@end
@implementation NoteObject
@dynamic remoteID;
@dynamic remoteArticleID;
@dynamic article;
@dynamic status;
@dynamic token;
@dynamic title;
@dynamic noteContent;
@dynamic pubDate;
@dynamic modDate;
@end
The offending line is in this block:
_noteObject = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:self.managedObjectContext];
_noteObject.remoteArticleID = 0; // this works
_noteObject.remoteArticleID = 1; // this crashes
What really has me stumped is that in another model I have the same fields with the same types and they will accept non-zero values without any trouble:
bookmarkObject = [NSEntityDescription insertNewObjectForEntityForName:@"Bookmark" inManagedObjectContext:self.managedObjectContext];
bookmarkObject.remoteArticleID = 0; // this works
bookmarkObject.remoteArticleID = 1; // this works, too
Is there anything in my .xcdatamodeld file that could be causing this?
EDIT
My data models look like this:
Upvotes: 4
Views: 1430
Reputation: 26
In your model file, check that the entity's "Class" property is set to the appropriate class, and not the default NSManagedObject
.
If you leave it as NSManagedObject
, Core Data will create properties itself on a custom NSManagedObject
subclass it generates itself, rather than using your own subclass. Most getters and setters will appear to work, but you may have issues with non-boxed primitive properties and custom getters and setters.
Upvotes: 0
Reputation: 4516
I had exactly the same problem.
It appears that xcode (or perhaps the compiler, or perhaps the two between them) sometimes gets confused when you manually edit properties in the NSManagedObject - it ends up treating our integers as pointers and trying to access memory directly - hence the EXC_BAD_ACCESS.
Anyway, as this question explains: SO Question, the solution is to delete your old class (obviously copy out any custom code so you can paste it back again later) and then get xcode to regenerate it for you (select the entity in the data model and select "Editor / Create NSManagedObject subclass..."). In the dialogue that appears, make sure "Use scalar properties for primitive data types" is ticked.
You may have to manually edit the resulting class to turn some non scalar properties back into objects (I had a date object which it turned into something other than NSDate - I forget exactly what, but it accepted the manually made edit back to NSDate).
It worked for me. Hope it works for you.
Ali
Upvotes: 5
Reputation: 750
Well, in case anyone else is having this issue, I never found a satisfactory answer for why one entity was working and the other wasn't. My workaround was to refactor the properties to use NSNumber
wrappers instead of primitive int64_t
values.
@property (strong, nonatomic) NSNumber *remoteID;
@property (strong, nonatomic) NSNumber *remoteArticleID;
Of course, that means boxing/unboxing the integer values.
_noteObject.remoteArticleID = [NSNumber numberWithInt:1];
int intVar = [_noteObject.remoteArticleID intValue];
Upvotes: 2