admin管理员组

文章数量:1405392

I would like to create a temporary BOOL property in a custom NSManagedObject class to track whether an async download of a remote image is underway so as not to start a second download while the first is underway. This value would not be stored in Core Data and is not included in xcmodel. It is supposed to be short term state variable to prevent recursive downloads.

In the past, I've often added properties to custom NSManagedObject class files with no problem. However, in this case, the managedobjects are being fetched with a FetchedResultsController into a tableview. Aalthough adding a BOOL to the class gets past the compiler it crashes at runtime.

When I look at the custom class in the debugger, it appears the BOOL property is not listed. Could it be that when you fetch managed objects using a fetchedResultsController, it ignores any properties that are not in the xcmodeldata file?

Here is the error message(abbreviated):

CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[Feed awaitingResponse]: unrecognized selector sent to instance 0x6000021d8910 with userInfo (null)
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Feed awaitingResponse]: unrecognized selector sent to instance 0x6000021d8910'

Here is my code.

Feed.h

@property (nonatomic, assign) BOOL awaitingResponse;

Feed.m

@dynamic awaitingResponse;

- (void)awakeFromInsert {
    self.awaitingResponse = NO;//Attempt to set a default value. Compiles okay.
}

The crash occurs, although the following compiles, when I try to access the property from another class as follows:

@implementation FeedViewController

- (void)configureCell:(FeedCell *)cell withFeed: (Feed *)aFeed {
    BOOL awaitingResponse = aFeed.awaitingResponse;//CRASH OCCURS HERE
}

I would like to use this BOOL to condition remote fetches of data, however, I can't even try to access it without getting the crash at runtime.

How can I get and set values of this property?

I would like to create a temporary BOOL property in a custom NSManagedObject class to track whether an async download of a remote image is underway so as not to start a second download while the first is underway. This value would not be stored in Core Data and is not included in xcmodel. It is supposed to be short term state variable to prevent recursive downloads.

In the past, I've often added properties to custom NSManagedObject class files with no problem. However, in this case, the managedobjects are being fetched with a FetchedResultsController into a tableview. Aalthough adding a BOOL to the class gets past the compiler it crashes at runtime.

When I look at the custom class in the debugger, it appears the BOOL property is not listed. Could it be that when you fetch managed objects using a fetchedResultsController, it ignores any properties that are not in the xcmodeldata file?

Here is the error message(abbreviated):

CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[Feed awaitingResponse]: unrecognized selector sent to instance 0x6000021d8910 with userInfo (null)
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Feed awaitingResponse]: unrecognized selector sent to instance 0x6000021d8910'

Here is my code.

Feed.h

@property (nonatomic, assign) BOOL awaitingResponse;

Feed.m

@dynamic awaitingResponse;

- (void)awakeFromInsert {
    self.awaitingResponse = NO;//Attempt to set a default value. Compiles okay.
}

The crash occurs, although the following compiles, when I try to access the property from another class as follows:

@implementation FeedViewController

- (void)configureCell:(FeedCell *)cell withFeed: (Feed *)aFeed {
    BOOL awaitingResponse = aFeed.awaitingResponse;//CRASH OCCURS HERE
}

I would like to use this BOOL to condition remote fetches of data, however, I can't even try to access it without getting the crash at runtime.

How can I get and set values of this property?

Share Improve this question edited Mar 22 at 16:12 user6631314 asked Mar 22 at 15:31 user6631314user6631314 1,9781 gold badge18 silver badges60 bronze badges 1
  • What's a point of adding dynamic property? You can add property in model and set it default value and get generated by xcode h,m files. – Cy-4AH Commented Mar 24 at 15:50
Add a comment  | 

1 Answer 1

Reset to default 0

Using @dynamic means you are planning to add implementations of the getter/setter methods at runtime, the way CoreData usually does (via low-level runtime stuff like -resolveInstanceMethod). If you don't, they won't exist despite the declaration in the .h saying they do, so you will get an exception if you try to call them at runtime.

Simply remove the @dynamic line -- the default should cause the compiler to generate the normal getter/setter methods and I think it should work. @dynamic is for properties that CoreData knows how to manage at runtime.

本文标签: