How to fetch a custom attribute from Kinvey - ios

I am looking to fetch a custom attribute I added to a user called "PostCount". I am looking to fetch it and set it to a label to view the post count. I successfully can increment the active user's PostCount but am unsuccessful at fetching it to display it.
Here is what I think I should be using.
var query = KCSQuery(onField: "PostCount", usingConditional: KCSQueryConditional.KCSAll, forValue: 4)
store.queryWithQuery(query, withCompletionBlock: "Not sure what to do here",withProgressBlock: "or here")
I think I need to use this, but I am not sure what the completionBlock or progressBlock would be.

Because it was a custom attribute under the user, it did not use the KCSQuery(). Instead I did the following
KCSUser.activeUser().getValueForAttribute("PostCount")
This retrieved the number stored in my post count, which I then set to the label I was displaying.

Related

Only writing data to firebase if information doesn't exist - Swift iOS

I want to only update the values email, firstname and lastname if they are blank.
I need this so that if the user decides to change these in the settings, they are not overwritten every time the user logs in with facebook.
Any solutions to check if the fields are blank without a datasnapshot? Trying to maximise efficency.
Current code when user signs in with facebook?
Database Structure for each user:
One way to do this is using a firebase transaction.
A transaction allows you to check the current value of a DB reference before you set/update it. It's main use case is preventing multiple concurrent updates from multiple sources but it can be used for this case as well - read and then write.
In the transaction block you get the value of the DB ref you're transacting on & can check that the value is null (hence 'create' case) -> then update it as required and return TransactionResult.success(withValue: newData).
If the object is already set you simply abort the transaction with TransactionResult.abort() and no write to the DB is executed.
Another option, that doesn't require a read/write, is to set a Firebase database rule on the relevant ref that will only allow write if the previous value was null:
"refPath": {
".write": "data.val() == null && newDataval() != null"
}
Writing a second time to the DB for an existing ref will fail.
I'd go with the transaction - more expressive of the requirement in the client code.
In firebase the only way you have to check if the current value of your fields in your database are empty is to fetch them before you are setting them.
You can check the field is empty only by fetching them.Then Use this code to update a particular value
ref.child("yourKey").child("yourKey").updateChildValues(["email": yourValue])

Firebase observeEventType .childAdded doesn't work as promised

When I call this observe function from in my viewcontroller, the .childadded immediately returns a object that was already stored instead of has just bin added like .childadded would suspect.
func observe(callback: RiderVC){
let ref = DBProvider.Instance.dbRef.child("rideRequests")
ref.observe(DataEventType.childAdded) { (snapshot: DataSnapshot) in
if let data = snapshot.value as? NSDictionary {
let drive = cabRide(ritID: ritID, bestemming: bestemming,
vanafLocatie: vanaf, taxiID: taxiID, status: status)
print(drive)
callback.alertForARide(title: "Wilt u deze rit krijgen?", message: "Van: \(vanaf), Naar: \(bestemming)", ritID: ritID)
}
}
}
When I try this function with .childchanged, I only get a alert when it is changed like it suppose to do, but when doing .chiladded, it just gets all the requests out of the database and those requests were already there.
When I add a new request, it also gives an alert. So it works, but how can I get rid of the not added and already there requests?
Does anybody know this flaw?
This is working exactly as promised. From the documentation:
Retrieve lists of items or listen for additions to a list of items.
This event is triggered once for each existing child and then again
every time a new child is added to the specified path. The listener is
passed a snapshot containing the new child's data.
That might seem weird at first, but this is generally what most developers want, as it's basically a way of asking for all data from a particular branch in the database, even if new items get added to it in the future.
If you want it to work the way you're describing, where you're only getting new items in the database after your app has started up, you'll need to do a little bit of work yourself. First, you'll want to add timestamps to the objects you're adding to the database. Then you'll want to do some kind of call where you're asking to query your database by those timestamps. It'll probably look something like this:
myDatabaseRef.queryOrdered(byChild: "myTimestamp").queryStarting(atValue: <currentTimestamp>)
Good luck!

Firebase fetch multiple results

I was wondering if there is any way with swift and firebase, to get multiple numbers from the same child? and average all the numbers.
Example,
Every authorized user saves a number to the database. Then when they look at there number in a saved section it averages all other users numbers and compares it against theres.
I can get all the data to save, and I can get the data to send back info, but it only sends one number from the list
I have tried different ways but I can't get it to work. Any help would be awesome.
I would post some code but none of it is working real well
Thanks again in advance
For your example, you would use childByAutoID to create a random unique identifier for your user. To get multiple values from one child you would use a comma, when setting the childs value. Like this,
struct getInput {
var inputType: String!
}
//copying and pasting this code into your code will not work, however
//you should be able to understand why and how this works.
let getUsername = getInput(inputType: usernameTextField.text!)
let getPassword = getInput(inputType: passwordTextField.text!)
let userReference = database.reference().child("users")
userReference.setValue(getUsername.inputType)
let childByAutoID = userReference.childByAutoId()
childByAutoID.setValue(["username": getUsername.inputType, "password": getPassword.inputType])

Firebase: Swift - Query value in child node without knowing the parent key

I have a firebase database which I have imported a list of approx. 8200 Universities with the details seen above.
I want to be able to enter an email address into a text field then query the "domain" part of this JSON. My issue is I need to query all the domains in the list of 8000, however I do not know how to search domain ad I do not have the "7542" to use a childByAppendingPath.
So I have a reference to "universities" but I need to be able to query "domain" without knowing the parent key (i.e. 7542 in the example above, but I want to search the domain, "iautb.ac.ir").
You need to create a reference and Query that reference, check this link to find more about how to retrieve data from firebase.
Basically you need to do something like this
let myRef = Firebase(url: "https://your.firebaseio.com/universities")
let query = myRef.queryOrderedByChild("domain").queryEqualToValue("yourDomainSearchValue")
and then observe the events you need to on your query

QuickBlox update custom object without using ID

I use QuickBlox for my chat app.
When updating/deleting a custom object, is it possible not to use object.ID?
For example, I use a custom field "requestor_id" and "responder_id" to identify FriendRequest Custom Object. I need to update the status of a friend request to from "pending" to "approved".
I don't want to fetch the ID first in order to update it.
Is it possible to send along a dictionary like #{"responder_id": 123, #"requestor_id":234}?
I think it's not possible. Deleting or updating take place only with object's ID.. so you need perform at last 2 actions:
fetch items
updating/deleting

Resources