Master data EXC_BAD_ACCESS for non-zero integer values

I have two main data models with int64_t properties. One of them works fine, and the other throws EXC_BAD_ACCESS when I try to assign a nonzero value to an integer field. I read the answers that say to recreate the NSManagedObject child class, and I did this without success. A broken class is as follows:

@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 violation line is in this block:

 _noteObject = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:self.managedObjectContext]; _noteObject.remoteArticleID = 0; // this works _noteObject.remoteArticleID = 1; // this crashes 

What really puzzled me was that in another model, I have the same fields with the same types, and they will receive non-zero values ​​without problems:

 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 can cause this?

EDIT

My data models are as follows:

NoteObject Data ModelBookmarkObject Data Model

+4
source share
3 answers

Well, if someone else is facing this problem, I never found a satisfactory answer why one person works and the other doesn't. My workaround was to reorganize the properties to use NSNumber wrappers instead of primitive int64_t values.

 @property (strong, nonatomic) NSNumber *remoteID; @property (strong, nonatomic) NSNumber *remoteArticleID; 

Of course, this means boxing / unpacking integer values.

 _noteObject.remoteArticleID = [NSNumber numberWithInt:1]; int intVar = [_noteObject.remoteArticleID intValue]; 
+2
source

I had exactly the same problem.

It seems that xcode (or maybe the compiler, or maybe two of them) is sometimes confused when you manually edit the properties in NSManagedObject - it finishes processing our integers as pointers and tries to access memory directly - hence EXC_BAD_ACCESS.

In any case, since this question explains: SO Question , the solution is to remove the old class (obviously copy any custom code so you can paste it back again) and then get xcode to regenerate it (select the object in the model data and select "Editor / Subclass NSManagedObject ..."). In the dialog that appears, make sure that "Use scalar properties for primitive data types" is checked.

You may need to manually edit the resulting class to turn some non-scalar properties back into objects (I had a date object that it turned into something other than NSDate). I forgot what it was, but he accepted the manual editing back by NSDate).

It worked for me. Hope this works for you.

Ali

+5
source

In your model file, make sure that the class property is set to the appropriate class, not the default NSManagedObject .

Entity Settings

If you leave it as NSManagedObject , Core Data will create the properties in the custom subclass of NSManagedObject , which it generates itself, instead of using your own subclass. Most recipients and setters seem to work, but you might have problems with primitive non-box properties and with custom getters and setters.

0
source

Source: https://habr.com/ru/post/1488579/


All Articles