simonbs
simonbs

Reputation: 8042

NSManagedObject property is null

I have a UIButton subclass in which I set a property concert to a subclass of NSManagedObject. When I set this property, I am sure that the NSManagedObject is not null and that it's properties aren't null. Also, the data of the object shows the correct data.

When using the concert later when the button is clicked and sent to the method as sender, I will cast the sender to my subclass of UIButton and the properties of the concert will now return null but the concert itself is not null. Also, the data will be fault.

Does anyone know why this happens and how I can fix this?

Here, I initialize my button:

NFConcertButton *button = [NFConcertButton buttonWithConcert:concert tileSize:self.tileSize];

In initWithButton:tileSize: (which is called by the static method) I store the concert for later use.

- (id)initWithConcert:(NFConcert *)concert tileSize:(CGSize)tileSize
{
    if (self = [super init])
    {
        // Store concert
        _concert = concert;

       /*
         <NFConcert: 0xde67eb0> (entity: Concert; id: 0xde63f60 <x-coredata://F027F762-2F30-4A43-898B-42ECC199DE97/Concert/p2> ; data: {
           band = "SomeBand";
         })
        */

        // concert is not null
        // concert.band is not null

        // .... //
    }

    return self;
}

When the button is pressed, the below method is called and the properties of concert are now null.

- (void)concertButtonPressed:(id)sender
{
    NFConcert *concert = ((NFConcertButton *) sender).concert;
    // <NFConcert: 0xde67eb0> (entity: Concert; id: 0xde63f60 <x-coredata://F027F762-2F30-4A43-898B-42ECC199DE97/Concert/p2> ; data: <fault>) => (null)
    // concert is not null.
    // concert.band is now null.
}

UPDATE:

If I fetch the NSManagedObject again using the objectId, I will get the data and the properties will not return null. I don't understand why this would be necessary. Can anyone tell me?

The below will work.

NFConcert *concert = ((NFConcertButton *) sender).concert;
concert = (NFConcert *) [managedObjectContext existingObjectWithID:concert.objectID error:nil];

Upvotes: 1

Views: 612

Answers (1)

simonbs
simonbs

Reputation: 8042

Using Apples default implementation of the Core Data stack, I no longer have this issue. I believe this issue was caused by DataManager which I used to work with the Core Data stack.

Upvotes: 1

Related Questions