Resolved How do I check if NSManagedObject has value for a given attribute?

Discussion in 'iOS Programming' started by moonman239, Jul 31, 2015.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    I'm developing a whole new app that's a game. In this game, multiple users use the same device, one user at a time. Users will unlock each level by completing the preceding one. I have a Core Data model that my app will use to keep track of which level the user has unlocked. Prior to playing any level, the user must select that level from a menu.

    So, I have an NSManagedObject representing the user. This object stores data on which level the user unlocked. If no such data is available, it's because the user hasn't completed a level. The problem is that
    Code:
    [self valueForKey:@"key"]
    
    causes an EXC_BAD_ACCESS code 2 when the app checks to see which level the user is on.
     
  2. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #2
  3. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    Yes, I've used zombies before.
    "self" refers to the managed object (Menu view controller basically asks for an object of a certain type,then the managed object is to retrieve an NSData and unarchive it to get the desired object.
     
  4. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #4
    Ok, so you are trying to retrieve an object from Core Data and the call to get the data crashes.

    You mention that the data may or may not be there, and this is the flag to indicate if a level has been completed.

    Is the crash related to the data not being there? In other words, does it work if the data is there and crashes if the data is not there?

    Isn't EXE_BAD_ACCESS something to do with an invalid pointer? I really don't know the error messages, but there has to be some docs on it somewhere.

    Has there been a change in the structure of the object that you are getting?

    Is there a native data type vs an non-native that needs to be converted?
     
  5. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #5
    Here's what's supposed to happen:
    1) User launches app.
    2) User selects his name from a list
    3) The level menu view controller requests, from the associated NSManagedObject, data that indicates which levels the user has unlocked. This data is stored in a key - which we shall call "level" - and is of the Binary Data type. However, there is a property by the same name in my NSManagedObject subclass that is of a different type. That's the way I wanted it because I wanted the managed object to archive/unarchive this property.

    There was a change in the model, but I think I deleted the app after making the change.
     
  6. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #6
    I'm not an expert on the error messages, so what I would do myself is to write a new program that stores and retrieves the exact same data from a core data store.

    There are many examples out there using core data, I would get some of those and see if you can change the data they use in their example over to the types of data you use.

    In other words, it looks like the problem is in understanding how to use core data and you're getting a cryptic error message. There are sample apps that show exactly how to use core data and I would use those to determine what's going wrong with your setup.

    Unless someone knows that message or if there's access to a more detailed error message, that's seems like the best approach.

    I've only done a few tutorials on Core Data, so maybe someone else knows the error messages.

    One other idea. Does the object have to be allocated before you get it from the data store or is it self allocating? Maybe Core Data is trying to put data into an address that isn't setup or doesn't have allocated memory.
     
  7. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #7
    I fixed the error by using
    Code:
    [self primitiveValueForKey]
    . Of course, then I had to use
    Code:
    [self setPrimitiveValue:forKey:
    instead of
    Code:
    [self setValue:forKey:]
    in the setter method.
     
  8. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #8
    Ok, so that means the problem was an object type vs a primitive type?

    Were you trying to put a primitive type into an object?

    The reason I ask is that people will search this thread to find answers to their problems.
     

Share This Page