Start a new topic
Answered

Cant use relationships with swift

I tested setting up a project exactly as showed in the ios guide for the data store relationships, see code below. I set up the database with one user, one event and one invitation. I get the following error: 'NSInvalidArgumentException', reason: '*** -[__NSSetM addObject:]: object cannot be nil'. Is it something I have missed here?


  

/// Invitation.swift
class Invitation : NSObject {
    var objectId: String?
    var status: NSInteger?
    var invitee: KCSUser?
    var event: Event?
    
    internal override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {
        return [
            "objectId" : KCSEntityKeyId,
            "status" : "status",
            "invitee" : "invitee",
            "event" : "event"
        ]
    }
    internal static override func kinveyPropertyToCollectionMapping() -> [NSObject : AnyObject]! {
        return [
            "invitee" : KCSUserCollectionName,
            "event" : "Events"
        ]
    }
}

/// Event.swift
class Event : NSObject {
    var entityId: String? //Kinvey entity _id
    var name: String?
    var date: NSDate?
    var location: String?
    var metadata: KCSMetadata? //Kinvey metadata, optional
    var invitations: NSMutableSet?
    
    internal override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {
        return [
            "entityId" : KCSEntityKeyId, //the required _id field
            "name" : "name",
            "date" : "date",
            "location" : "location",
            "invitations" : "invitations",
            "metadata" : KCSEntityKeyMetadata
        ]
    }
    internal static override func kinveyPropertyToCollectionMapping() -> [NSObject : AnyObject]! {
        return [
            "invitations": "Invitations"
        ]
    }
    internal static override func kinveyObjectBuilderOptions() -> [NSObject : AnyObject]! {
        return [
            KCS_REFERENCE_MAP_KEY : [
                "invitations" : Invitation.self
            ]
        ]
    }
}

/// AppDelegate.swift
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        KCSClient.sharedClient().initializeKinveyServiceForAppKey(
            "kid_W1rh0mjWXZ",
            withAppSecret: "ec71b5ea79cf41d6ab94adf608726e27",
            usingOptions: nil
        )
        
        let store = KCSLinkedAppdataStore.storeWithOptions([
            KCSStoreKeyCollectionName : "Events",
            KCSStoreKeyCollectionTemplateClass : Event.self
        ])
        let eventId = "5742372fe6eff86975870547" //change to a real id
        store.loadObjectWithID(
            eventId,
            withCompletionBlock: { (objectsOrNil: [AnyObject]!, errorOrNil: NSError!) -> Void in
                if errorOrNil == nil {
                    let event = objectsOrNil[0] as! Event
                    let invite = event.invitations!.anyObject() as! Invitation // will be realized Invitation object
                    print(invite)
                }
            },
            withProgressBlock: nil
        )
        
        return true
    }

  


Best Answer

Hey Simon,


It seems like you don’t have an active user - the one who is logged into the app.
The app needs an active user.

Please take a look at the following link:
http://devcenter.kinvey.com/ios/guides/users#ActiveUser

In short, you will need to login to get the active user context:

 

KCSUser.loginWithUsername(
            “xxxx",
            password: “xxxx",
            withCompletionBlock: { (user: KCSUser!, errorOrNil: NSError!, result: KCSUserActionResult) -> Void in
                if errorOrNil == nil {
                    //the log-in was successful and the user is now the active user
                  
            // DO YOUR LOGIC HERE    
                }
            }
        ) 

 

Thanks,

Pranav

Kinvey Support

1 Comment

Answer

Hey Simon,


It seems like you don’t have an active user - the one who is logged into the app.
The app needs an active user.

Please take a look at the following link:
http://devcenter.kinvey.com/ios/guides/users#ActiveUser

In short, you will need to login to get the active user context:

 

KCSUser.loginWithUsername(
            “xxxx",
            password: “xxxx",
            withCompletionBlock: { (user: KCSUser!, errorOrNil: NSError!, result: KCSUserActionResult) -> Void in
                if errorOrNil == nil {
                    //the log-in was successful and the user is now the active user
                  
            // DO YOUR LOGIC HERE    
                }
            }
        ) 

 

Thanks,

Pranav

Kinvey Support

Login or Signup to post a comment