Parse to Kinvey Query - swift

I have just migrated from parse to kinvey as a result of the shutdown and i was wondering if anybody could help me figure out how this query would look like with the kinvey SDK. Here it is:
let innerP1 = NSPredicate(format: "sender = %# AND other = %#", userName, otherName)
let innerQ1:PFQuery = PFQuery(className: "Messages", predicate: innerP1)
let innerP2 = NSPredicate(format: "sender = %# AND other = %#", otherName, userName)
let innerQ2:PFQuery = PFQuery(className: "Messages", predicate: innerP2)
let query = PFQuery.orQueryWithSubqueries([innerQ1,innerQ2])
query.addAscendingOrder("createdAt")
query.findObjectsInBackgroundWithBlock {
(objects:[PFObject]?, error:NSError?) -> Void in //UPDATE THIS
}

As a broad example...
http://devcenter.kinvey.com/phonegap/reference/api/Kinvey.Query.html
var query = new Kinvey.Query();
var query2 = new Kinvey.Query();
query.equalTo("sender",userName).and().equalTo("other",other name);
query2.equalTo("sender",otherName).and().equalTo("other",userName);
query.or(query2);
query.ascending("_kml.lmt"); //last modified time
var promise = Kinvey. DataStore.Find("group name",query);
promise.then(successfunction,fail function);
Also please note this is an example with the phonegap library, please see the kinvey reference for Android or iOS for your situation.

Related

Swift & Parse: Query Results Where Column Class Is Equal To A Column In Users Class

I have searched far and wide for an answer to this. In written English I'm trying to do the following:
Query results where the column "WorkGroup" in the "Issues" class is equal to the column "WorkGroup" in the "Users" class.
I tried countless things including; NotContainedIn, ObjectForKey, wherekey (matchesquery), includeKey, add pointers, etc.
let query = PFQuery(className: ISSUES_CLASS_NAME)
let now = Date()
query.whereKey(ISSUES_SUB_DATE, lessThanOrEqualTo: now)
query.whereKey(ISSUES_STATUS, notEqualTo: "Closed")
query.order(byDescending: ISSUES_SUB_DATE)
// Add Where Issues WorkGroup value = Users WorkGroup value.
let userQuery = PFUser.query();
// Set up user query
let issueQuery = PFQuery(className:"Issue");
issueQuery.whereKey("WorkGroup", matchesKey:"WorkGroup", inQuery:userQuery);
http://parseplatform.org/Parse-SDK-iOS-OSX/api/Classes/PFQuery.html#/c:objc(cs)PFQuery(im)whereKey:matchesKey:inQuery:
Try this
func yes(){
let query = PFUser.query()
query?.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
if error == nil{
for object in objects!{
self.array.insert(object.object(forKey: WorkGroup) as! String, at: 0)
let dataQuery = PFQuery(className: "Issues")
dataQuery.whereKey(WorkGroup, containedIn: self.array)
dataQuery.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
if error == nil{
for object in objects!{
}
}
})
}
}
})
}
Double check the brackets

'Cannot do a comparison query for type: PFRelation' Swift

I am trying to query for all of the existing users in my app that the user has not added as a "Friend". I am getting the error
Cannot do a comparison query for type: PFRelation
Here is my current code:
override func queryForTable() -> PFQuery {
let currentFriends : PFRelation = PFUser.currentUser()!.relationForKey("friends")
// Start the query object
let query = PFQuery(className: "_User")
query.whereKey("username", notEqualTo: currentFriends)
// Add a where clause if there is a search criteria
if UserInput.text != "" {
query.whereKey("username", containsString: UserInput.text)
}
// Order the results
query.orderByAscending("username")
// Return the qwuery object
return query
}
How do I solve this issue? Thanks
I solved my problem.... in a way. I was unable to compare the PFRelation so I created a helper Parse class that saves the user who adds the other user as "from" and the user they add as "to" then I am able to compare them like this.
let currentUser = PFUser.currentUser()?.username
let currentFriends = PFQuery(className: "Friends")
currentFriends.whereKey("from", equalTo: currentUser!)
// Start the query object
let query = PFQuery(className: "_User")
query.whereKey("username", doesNotMatchKey: "to", inQuery: currentFriends)
I hope this helps someone in the future.

CKQuery with CloudKit Issue

I'm very new to CloudKit and am simply trying to run the most basic query. I want to pull from RecordType "Users" and field type "Name" and have the name equal to my nameText label!
Please see my code below:
func getUserInformation() {
let Pred = NSPredicate(value: true)
let Query = CKQuery(recordType: "Namer", predicate: Pred)
let AgeOp = CKQueryOperation(query: Query)
AgeOp.database = self.Pub
AgeOp.recordFetchedBlock = {(record : CKRecord!) in
let Recorded : CKRecord = record!
self.nameText.text = Recorded.objectForKey("Name") as? String
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.Container = CKContainer.defaultContainer() as CKContainer
self.Pub = Container.publicCloudDatabase as CKDatabase
self.Container.accountStatusWithCompletionHandler {
accountStatus, error in
if error != nil {
print("Error: \(error?.localizedDescription)")
} else {
self.getUserInformation()
}
}
}
Users is a system table. You could add new fields to that, but it's advised not to do so. One of the main reasons is that you cannot query the Users recordType.
Besides that I see in your code that you query the recordType Namer and not Users. Is your question wrong or is your code wrong?
In the recordFetchedBlock you directly put the result in the .text. That block will be executed in a background thread. You should first go to the mainQueue. You could do that like this:
AgeOp.recordFetchedBlock = {(record : CKRecord!) in
NSOperationQueue.mainQueue().addOperationWithBlock {
let Recorded : CKRecord = record!
self.nameText.text = Recorded.objectForKey("Name") as? String
}
}
When there is no query match, then the .recordFetchedBlock will never be called. You should also set the .queryCompletionBlock
The query could return multiple records (if there are more people with the same name). In the recordFetchedBlock you should add records to an array. Then in the .queryCompletionBlock you could use that array.

swift load all record from cloud kit avoiding limit result

guys need help i can't find something similar online even setting the limit result to a fixed value still doesn't work no matter what i always get 100 record max i need all the records in my database right now is 377 record and will be way more probably 10k but this process will only run once could someone help me catch all the record from the database avoiding the 100 limit please
i added the cursor as you suggested and now i have an idea but now it loads 200 results not the total any tip please
if foodList.count == 0{
loadingView.hidden = false
loadMainDatabaseActivity.startAnimating()
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "FoodListDataBase", predicate: predicate)
let operation = CKQueryOperation(query: query)
//operation.resultsLimit = CKQueryOperationMaximumResults
operation.recordFetchedBlock = { (record: CKRecord!) in
if record != nil{
count++
foodList.append(record.objectForKey("foodname") as! String)
categories.append(record.objectForKey("category") as! String)
}
}
operation.queryCompletionBlock = {(cursor: CKQueryCursor!, error: NSError!) in
if cursor != nil {
let newOperation = CKQueryOperation(cursor: cursor)
newOperation.recordFetchedBlock = operation.recordFetchedBlock
newOperation.queryCompletionBlock = operation.queryCompletionBlock
newOperation.resultsLimit = 300
publicDB!.addOperation(newOperation)
println(count)
}
self.loadingView.hidden = true
self.loadMainDatabaseActivity.stopAnimating()
}
publicDB.addOperation(operation)
}
I believe this was answered in the following post:
CKQuery from private zone returns only first 100 CKRecords from in CloudKit
Changing the CKQueryOperationMaximumResults to a larger number.

recordFetchedBlock on CKQueryOperation not being called

I need to query for CKRecords of the recordType "Event". When I call recordFetchedBlock, I will add those records to my array. However, recordFetchedBlock is never getting called. Please help! Thank you:
//TODO: Query for all Event records
var database: CKDatabase = CKContainer.defaultContainer().privateCloudDatabase
let truePredicate = NSPredicate(value: true)
let eventQuery = CKQuery(recordType: "Event", predicate: truePredicate)
let queryOperation = CKQueryOperation(query: eventQuery)
queryOperation.recordFetchedBlock = { (record : CKRecord!) in
self.eventsArray.append(record)
println("recordFetchedBlock: \(self.eventsArray)")
}
queryOperation.queryCompletionBlock = { (cursor : CKQueryCursor!, error : NSError!) in
println("queryCompletionBlock: \(self.eventsArray)")
}
database.addOperation(queryOperation)
So I just realized my error. I was accidentally calling the query on the private database (as opposed to the public). Currently kicking myself..