Parse: How can I update all rows with the same key value - ios

How can I update all rows with the same key value in a Parse class?
As my code, I think Parse would cost a lot of my money if there are 100 objects per user.
Is there any other way to do that?
let username = PFUser.currentUser()?.username
let objectQuery = PFQuery(className: "FriendList")
objectQuery.whereKey("username", equalTo: username!)
objectQuery.findObjectsInBackgroundWithBlock { (friendList:[AnyObject]?, error:NSError?) -> Void in
if let friendList = friendList as? [PFObject] {
for myInfo in friendList {
myInfo["contact"] = contact
myInfo["portrait"] = PFFile(data: portraitNSData)
myInfo["company"] = company
myInfo["position"] = position
myInfo.save() // create a request?
}
}
}

Form array of needed objects and than just
PFObject.saveAllInBackground(objectsArray)

Parse has a functions to object see to save many objects (see documentation here)
PFObject.saveAllInBackground(array, block: {
(succeeded: Bool, error: NSError!) -> Void in
if (error != nil) {
println("Error saving: \(error)")
}
})
Unfortunately the documentation is not update to Swift but you can see a list of the function in objective-c
saveAll:
saveAll:error:
saveAllInBackground:
saveAllInBackground:block:

It would be best to create a CloudCode function. Then call that function from iOS. It's not difficult, see their documentation here: Parse.com

Related

Struggling to append arrays across Parse classes

Hi guys I'm trying to build a simple swipe app to like and dislike uploaded photos. I'm struggling with adding the likes/dislikes to Parse the way that I want them to. I've tried two ways so far:
adding the objectId of the posted image to the User who liked/disliked it but the problem is only one of the objectId's shows up in the array.
staying in the Parse class where the images are posted to (Post), add the userID of the liker/disliker to the image. This doesn't happen at all, new rows are created with new objectId's everytime an image is liked/disliked.
Ideally I want the users who have liked/disliked the photo in a single array so I can query this later. I don't have a great understanding of Parse, it's my first time using it so any help will be massively appreciated.
Here is the code I'm using when an image is swiped (adding to Post class):
if gesture.state == UIGestureRecognizerState.Ended {
var likedOrDisliked = ""
if label.center.x < 100 {
print("Dislike")
likedOrDisliked = "disliked"
} else if label.center.x > self.view.bounds.width - 100 {
print("Like")
likedOrDisliked = "liked"
}
if likedOrDisliked != ""{
var post = PFObject(className: "Post")
post.addUniqueObjectsFromArray([(PFUser.currentUser()?.objectId!)!], forKey: likedOrDisliked)
post.saveInBackground()
}
This is the snippet of when I try adding to User class:
PFUser.currentUser()?.addUniqueObjectsFromArray([displayedUserID], forKey: likedOrDisliked)
do {
try PFUser.currentUser()?.save()
} catch {
}
Here is what happens in the dashboard,
new rows created
What you wanted is to update the actual Post with the like/dislike user
Create a Post (This part you have not explained but i am show a simple assumption - pseuodo code)
var post = PFObject(class:"Post")
post["image"] = PFFile(image)
post.save()
Next you show the image on screen by getting the image from the post
When the user dislikes/likes
you add the current PFUser to the liked/disliked column and save back the object.
let arrayMut = NSMutableArray()
var array = NSArray()
if let arrayData = post.objectForKey("likedUser") as? NSArray {
array = arrayData
}
loop through now the array to find if current user is there.. if not find .. add current PFUser
arrayMut.addObject(PFUser.currentUser().objectId);
post.setObject(arrayMut, forKey: "likedUser")
post.save()
I've tried a lot of things and eventually something stuck, the desired effect was achieved through (added the current user to the liked or disliked fields) :
if likedOrDisliked != ""{
var post = PFQuery(className: "Post")
post.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects {
for object in objects {
var objId = object["objectId"]
var query = PFQuery(className: "Post")
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error == nil {
object.addUniqueObjectsFromArray([(PFUser.currentUser()?.objectId)!], forKey: likedOrDisliked)
object.saveInBackground()
}
})
}
}
}
})

Searching Parse using pointer returns error "pointer field needs a pointer value"

I'm trying to do a query by selectedBook (this is a string of selectedBook's objectID). However, I'm getting this error: "[Error]: pointer field book needs a pointer value (Code: 102, Version: 1.8.2)." I believe I have to do the query with the actual book object rather than the objectID, how can I go about doing this? Thanks in advance!!
var query = PFQuery(className:"UserTags")
query.whereKey("book", equalTo:selectedBook)
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) tags.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
print(object.objectId)
}
}
} else {
// Log details of the failure
print("Error: \(error!)")
}
Figured it out. Here is the code in case someone else runs into this:
let pointer = PFObject(withoutDataWithClassName:"Book", objectId: booksObjectID)
var query = PFQuery(className: "UserTags")
query.whereKey("book", equalTo: pointer)
query.includeKey("book")
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
for object in objects! {
self.userTags.addObject(object)
}
}
From documentation:
InvalidQuery - 102 - Error code indicating you tried to query with a
datatype that doesn't support it, like exact matching an array or
object.
The error description tells you a lot. This kind of querying does not support queuing by object (in your case, book). You have to use a datatype that is supported - for example query by book ID or name which are more primitive data types.

How to create pointer to Parse PFUser from facebook ID

I'm doing a Facebook graph call to get friends of the user that are using my app. I get the facebook ID of the user's friends back from the graph call. Below is what I'm attempting to obtain from Parse with that ID, but's it's not getting all the users back, I believe since its an async call. How can I save an array of pointers of the user's fb friends that are using the app? Thanks in advance!!
graphConnection.addRequest(requestFriends, completionHandler: { (connection: FBSDKGraphRequestConnection!, result: AnyObject!, error: NSError!) -> Void in
if result.objectForKey("friends") != nil {
// parsing dictionary to get results
let firstDict = result.objectForKey("friends")
let dataArray = firstDict!.objectForKey("data")
let myFriendsUsingTheAppCount = dataArray!.count
print("\(myFriendsUsingTheAppCount)")
let friendsArray:NSMutableArray = []
for var i = 0; i < dataArray!.count; i++ {
let friend = dataArray![i]
let friendFbObjectID = friend.objectForKey("id")!
let query = PFUser.query()
query!.whereKey("facebookID", equalTo: friendFbObjectID)
query!.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error == nil {
// this is where I was going to save this specific user as a pointer
} else {
// some error
}
})
// this is saving just the friend's name/fb id to an array, but I want an array of pointers to the PFUser
friendsArray.addObject(friend)
}
} else {
// fb friends is nil
print("FB friends came back nil")
}
})
This could be simplified into a single query by using whereKey:containedIn:
let facebookIDs = dataArray.map { $0.objectForKey("id")! }
let query = PFUser.query()!
query.whereKey("facebookID", containedIn: facebookIDs)
The query will now contain all users whose facebook id is in the array passed to the query.
(swift syntax may be incorrect did not double check)

How can i extract data from an anyObject in Swift

I'm using the TwitterKit SDK and listing a group of Tweets. The function has an error handler that stores any tweets that have been removed by the user and thus not shown. I am attempting to retrieve these particular ID's from the NSError user info dictionary. I can find them but end up with an anyObject.
This code is getting the tweet objects and filtering out the bad ones...
// load tweets with guest login
Twitter.sharedInstance().logInGuestWithCompletion {
(session: TWTRGuestSession!, error: NSError!) in
// Find the tweets with the tweetIDs
Twitter.sharedInstance().APIClient.loadTweetsWithIDs(tweetIDs) {
(twttrs, error) - > Void in
// If there are tweets do something magical
if ((twttrs) != nil) {
// Loop through tweets and do something
for i in twttrs {
// Append the Tweet to the Tweets to display in the table view.
self.tweetsArray.append(i as TWTRTweet)
}
} else {
println(error)
}
println(error)
if let fails: AnyObject = error.userInfo?["TweetsNotLoaded"] {
println(fails)
}
}
}
The println(error) dump is...
Error Domain=TWTRErrorDomain Code=4 "Failed to fetch one or more of the following tweet IDs: 480705559465713666, 489783592151965697." UserInfo=0x8051ab80 {TweetsNotLoaded=(
480705559465713666,
489783592151965697
), NSLocalizedDescription=Failed to fetch one or more of the following tweet IDs: 480705559465713666, 489783592151965697.}
and refining the results from the error "error.userInfo?["TweetsNotLoaded"]" I can end up with...
(
480705559465713666,
489783592151965697
)
My question is, is there a better way to get this data?
If not, how am I able to convert the (data, data) anyObject into an array of [data, data] ?
Best guess is that TweetsNotLoaded is an NSArray of NSNumber (or maybe NSString, not sure how they're coding/storing message ids), so you can cast the result and go from there:
if let tweetsNotLoaded = error.userInfo?["TweetsNotLoaded"] as? [String] {
// here tweets not loaded will be [String], deal with them however
...
}
If that doesn't work, assume they're longs and use:
if let tweetsNotLoaded = error.userInfo?["TweetsNotLoaded"] as? [Int64] {
// here tweets not loaded will be [Int64], deal with them however
...
}

Parse: deleting object value in column using Swift

I've used Parse successfully in other apps before but never used the delete function. I'm trying to delete a value ( an alphabetical letter) in a column (column title is 'letter') associated with a user in Parse. I'm using Swift. The code is finding the correct value as evident via a println in the deletion code, but nothing is happening after the remove and save functions are executed. The value is still there in the column. And I'm not getting any Parse errors. The code is below. Any help, as always, will be greatly appreciated.
var query = PFQuery(className: "game")
query.whereKey("player", equalTo:PFUser.currentUser())
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if !(error != nil) {
for object in objects {
var myLetter = object["letter"]! as String
println("The object for key letter is \(myLetter)") //This prints the correct letter in the current user's Letter column
PFUser.currentUser().removeObjectForKey("letter")
PFUser.currentUser().saveInBackgroundWithBlock{
(success: Bool, error: NSError!) -> Void in
if (success) {
// The object has been saved.
println("success")
} else {
// There was a problem, check error.description
println(error)
}
}
}
}
}
I think the issue is that you are creating a new Parse query and deleting it locally as opposed to retrieving the item and then deleting it. So, retrieve the item you want to delete and then call the deleteInBackground method.

Resources