1

I've a small problem. I am working on an App using Parse backend. So my problem is: i've retrieved all the objectIds of users in _User class like this:

 var userIds = [String]()

   var userQuery = PFUser.query()
      userQuery?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in 

        if let users = objects {

            self.userIds.removeAll(keepCapacity: true)

        for object in users {

            if let user = object as? PFUser {

                if user.objectId != PFUser.currentUser()?.objectId {


                        self.userIds.append(user.objectId!)

                    }

                }

            }

        }

        println(userIds)
    })

Now i want to save all the userIds which are stored in the array "userIds" to a column in a class named "Something". so the code will be like:

 var something:PFObject = PFObject(className: "Something")
     something["users"] = // have problem here 

     something.saveInBackground()

if i put userIds here, it gives an error.. because userIds is an array but column "users" is String.. but also i want to save all the userIds seperately in the column "users" like it is saved in objectId column of _User class..

rici
  • 234,347
  • 28
  • 237
  • 341
Akshay Kheveria
  • 201
  • 1
  • 15
  • If I understand correctly, you would have to create as many "Something" class objects as you have user IDs and you would save every object individually, this way you could fill the table for the class "Something", or do you want to have one "Something" object with an array containing the user IDs? – TurboFish Jul 25 '15 at 21:38
  • i want to save every object individually in the "users" column of "Something" class.. The first one u talking about.. – Akshay Kheveria Jul 25 '15 at 21:51

2 Answers2

1

I'm new here and I can't comment in the reply where you asked for help. Anyway, what do you want now is save only new users, as you said:

I just want to save the users which are not there

Well, there are some suggestions:

  1. You can retrieve your User class and check for new users manually in the app and then send the new ones to your Class.

  2. You can create a new column in your User class like "saved" where contains a boolean indicating if you already saved this user to your custom class or not. Remember also, of always do

    user["saved"] = false

while creating a new user and then simply do:

let search = PFQuery(className: "_User")

search.whereKey("saved", equalTo: false)

to get these non saved users.

I strongly recommend the second option.

0

Ok, the case that you describe should look something like this:

var userIds = [String]()

   var userQuery = PFUser.query()
    userQuery?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

        if let users = objects as? [PFUser] {

            var somethingObjects = [PFObject]()

            self.userIds.removeAll(keepCapacity: true)

            for user in users {

                if user.objectId != PFUser.currentUser()?.objectId {

                    self.userIds.append(user.objectId!) // I don't know if this is necessary anymore for your usecase

                    var something:PFObject = PFObject(className: "Something")
                    something["users"] = user.objectId
                    somethingObjects.append(something)
                }

            }

            PFObject.saveAllInBackground(somethingObjects)
        }
    })

In this case, you create a number of something objects that are saved, but you might want to rename the "users" column to "user", since you only save one objectId per row.

I haven't worked with parse in a while, so there might be a way to optimize the number of API calls you have to make.

TurboFish
  • 7,789
  • 4
  • 18
  • 27
  • This Worked :D but there is one more problem i am stuck with now, This query is in my viewDidLoad method, and i want it to be there only, now whenever i open the app, it saves the same users again, so my problem is that it should not save all of them again and add the one's which are not there .. :/ – Akshay Kheveria Jul 25 '15 at 22:10
  • You could retrieve your "something" objects and check which ids are already added. Or you write a cloud function that deals with that problem on the server side. Would also reduce your API calls to 1. You would call this cloud function with the array of userIds you want to add, but this is a different problem I guess. :) If my answer was helpful, could you please mark this one as the answer? – TurboFish Jul 25 '15 at 22:14
  • Not able to check that :/ I just want to save the users which are not there.. :( – Akshay Kheveria Jul 25 '15 at 22:19
  • I am stuck here from a week now :-| – Akshay Kheveria Jul 25 '15 at 22:36