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..
Related
I have CKRecord for Location details in the public database. I want to create a subscription for a CKrecord with location details, stored in the public database. I want to set subscription so I get notification from those that are in my contacts group only. The subscription notification on Location CKrecord notifies me on all users which takes more time to sort through and I want to avoid this.
How can I set the subscription?
Assuming your Location recordType has some kind of userEmail field on it, you can query for those records like this:
let predicate = NSPredicate(format: "userEmail = %#", argumentArray: ["name#example.com"])
let query = CKQuery(recordType: "Location", predicate: predicate)
let operation = CKQueryOperation(query: query)
var records = [CKRecord]()
operation.recordFetchedBlock = { record in
records.append(record)
}
operation.queryCompletionBlock = { cursor, error in
print("Done.")
print(records)
}
CKContainer(identifier: "iCloud.yourContainer").publicCloudDatabase.add(operation)
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.
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.
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.
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.