1

I am trying to save a "Post Count" along with each user so I can keep track of the total amount of times they have posted. First, when I create a new column in the dashboard, it deletes the column once it refreshes. Also, I am curious on how to save it to Kinvey with Swift. Here is what I have thus far.

KCSUser.activeUser().setValue(num, forAttribute: "PostCount")
KCSUser.saveWithCompletionBlock(KCSUser.activeUser())

I realize I need a completion block, which is I think is between me and getting it to work. Thank you in advance!

  • Alternatively you could use a 'counting query' on Posts where the user is your active user. That saves you having to store redundant, repeated data for the user, and ensures that it is normalized. See http://devcenter.kinvey.com/ios/guides/datastore#counting for more info. – max_ Apr 10 '16 at 19:36
  • Right now I have it set up to increment when I click a button to just test it, rather than connecting it to when a post happens, which I don't have yet. Also, if I am understanding the documentation correctly, the counting you are talking about is the count of something in a collection, like an array of pictures someone has posted. –  Apr 10 '16 at 19:51
  • Yes assuming you have an array of posts in the database – max_ Apr 10 '16 at 19:52

1 Answers1

0

Although above I was just using that code to test whether I could save the new value after tapping an 'Increment' button which in turn would test to see if it was saving the new value in Kinvey, I ended up finding out how to finish that test.

KCSUser.activeUser().setValue(num, forAttribute: "PostCount")
KCSUser.activeUser.saveWithCompletionBlock { (error) -> Void in
    print(error)
}

In my original question, it was crashing when pressing the increment button. In the future, this code needs to be put into the area where you are posting so that it increments once a post is posted, rather than when you click an test increment button.