Kinvey 3.3.5 SDK Query By Ids

140 views Asked by At

I am currently going through the processes of migrating swift 2.3 to 3 using the most updated Kinvey SDK (version 3.3.5). They have done a ton of updates since the 1x versions. My question is has anyone successfully been able to query on the PersistableKeyID field and pull multiple objects?

I use to be able to use the "loadObjects" function which would take an array of strings as an argument. This function has since been depreciated and replaced with find(byId). See below:

dataStore.find(byId: "only takes one") { uClass, error in
if let uClass = uClass {
    //succeed
    print("UClass: \(uClass)")
} else {
    //fail
} 

The issue is, it will only take a single string as an argument. I have attempted to use the query functionality, but I cannot get it to take the "_id" field as a parameter. Using the following code:

 //Just statically creating the sectionID array for now.  This will dynamically be created
    testIDs = ["58668307206c11177e5ab0d4", "58668307206c11177e5ab0d4", "57ad00a505a2bb55632659c3"]

    let sectionStore = DataStore<Section>.collection()

    let sectionQuery = Query(format: "_id IN %@", testIDs)

    sectionStore.find(sectionQuery) {sectionResult, error in
        if let sectionResult = sectionResult {
            self.sectionsTest = sectionResult

            self.sectionCollectionView.reloadData()

        } else{
            //Error
        }

    }

I receive the error:

'Invalid property name', reason: 'Property '_id' not found in object of type 'Section'

Anyone have an idea on how to perform this now that "loadObjects" has been depreciated? There is no delivered "find(byIds)" that I could find.

2

There are 2 answers

3
Pranav Jadhav On

Jbone107,

I was able to get results with this, let me know if the below works for you.

    let id:[String] = ["5855026650a816ec29012908","5855024a21400c5b492bea20"]

    let query = Query(format: "_id IN %@", id)

    dataStore.find(query) { data, error in
        if let data = data {
            //succeed
             print(“Data: \(data)")

        } else {
            //fail
            print("fetching failed")
        }
    }

Thanks, Pranav, Kinvey

0
jbone107 On

Answered: Per the Data Store Guide for iOS, by default the ".collection()" is of type "cache". The "Cache" type will store data locally. This must be why "Realm" is now included with the version 3x SDK.

I updated my DataStore collection to:

let sectionStore = DataStore<Section>.collection(.network)

I added ".network" to force the query to pull from the backend rather than the cache file. This actually identified "_id" as a property and the query worked successfully. For some reason the "cache" file isn't storing this as a property.

Additional SDK Question Answered

I was having an issue pulling NSNumber from the Kinvey backend. This ended up being a similar issue related to the "cache" query. I reviewed the Realm support site as a last resort effort to try and figure this out. I found that Realm doesn't actually support type "NSNumber".

Excerpt taken from: https://realm.io/docs/swift/latest/ Realm supports the following property types: Bool, Int8, Int16, Int32, Int64, Double, Float, String, NSDate, and NSData.

Unfortunately, Kinvey doesn't support "Int" types. As a work around, I have changed them to string and am just converting back to "Double" or another type after I pull the data. However, if I just use ".network" collection types, then NSNumber still works.

Thanks, James