Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data EXC_BAD_ACCESS for non-zero integer values

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:

NoteObject Data ModelBookmarkObject Data Model

like image 285
Raider Avatar asked Mar 23 '23 09:03

Raider


1 Answers

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

like image 145
Ali Beadle Avatar answered Apr 02 '23 11:04

Ali Beadle