Change value of all objects from query. [parse.com] - ios

Current code:
var query2 = PFQuery(className: "Post")
query2.whereKey("alias", equalTo: self.currentname)
query2.findObjectsInBackgroundWithBlock { (objectos, NSError) -> Void in
println(objects.count)
if objectos.count != 0 {
for objecto in objectos {
}
})
I want to change the value of a column in objecto for all objectos. I tried:
objecto["alias"]! = self.usernametextfield.text as AnyObject
But it doesn't work. So to clarify, I am querying Post, to find all objects where alias is equal to self.currentname. I want to change a string column of all objects found to self.usernametextfield.text.

Related

Is it possible to query dictionary on parse.com?

i’m trying to search dictionary match result but no luck
my parse dictionary column look like this
columnName: Tag property: object
{"firstKey”:”David”,”secondKey”:”Guetta”}
columnName: name property: string
cool

when I try to search name column
here is my code snippet,
static func parseQueryDictionary() {
let query = PFQuery(className:"TestDictionary")
query.whereKey("name", equalTo: "cool")
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil && objects != nil {
print("objects is", objects)
} else {
print(error)
}
}
}
i get result below
objects is Optional([ {
Tag = {
firstKey = David;
secondKey = Guetta;
};
name = cool;
tagArray = (
David,
Guetta
);
}])
i've try array column
columnName: tagArray property: array
["David","Guetta"]
static func parseQueryDictionary() {
let query = PFQuery(className:"TestDictionary")
query.whereKey("tagArray", equalTo:"David")
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil && objects != nil {
print("objects is", objects)
} else {
print(error)
}
}
}
i get result
objects is Optional([ {
Tag = {
firstKey = David;
secondKey = Guetta;
};
name = cool;
tagArray = (
David,
Guetta
);
}])
but when i try to search dictionary column
columnName: Tag property: object
{"firstKey”:”David”,”secondKey”:”Guetta”}
like this
static func parseQueryDictionary() {
let query = PFQuery(className:"TestDictionary")
query.whereKey("Tag", equalTo:"David")
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil && objects != nil {
print("objects is", objects)
} else {
print(error)
}
}
}
i get no result
objects is Optional([])

i’ve try google and parse official doc but can’t find this case, is it possible to do that?
i've try search string column, array column it's work but only dictionary column not work...
search in google with "findObjectsInBackgroundWithBlock" in swift.It will give you many results.
var query = PFQuery(className: parseClassName)
query.whereKey("Position", equalTo: "iOS Developer")//Here Position is column name and Sales Manager is value.
query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
if(error == nil){
for object in objects {
}
}
else{
println("Error in retrieving \(error)")
}
})

How do i set value for all objects in background? [parse.com]

Heres my code so far:
var query2 = PFQuery(className: "Post")
query2.whereKey("alias", equalTo: self.currentname)
query2.setValue(self.usernametextfield.text, forKey: "alias")
query2.findObjectsInBackgroundWithBlock { (objects, NSError) -> Void in
for objects in objects {
}
}
}
I want to set objects["alias"] equal to a string for all the objects retrieved from the query. How do I do this?

Post rating system in iOS with parse.com - upvote / downvote

Simply: I want rating system of the post (or image) that works the same as in Yik Yak or 9gag - user can upvote or downvote only once the post. I dont want to store these data on device but everything on parse.com. I also would like to count the comments. Ideally do everything in only one query to parse.com
I am using currently this code but I dont think this is the good approach how to do it because for every image it will create a new query. Currently I have 4 tables in Parse.com User, Image, Comment, Rating and everything is connected with pointers to the image or user.
func loadData(){
forPaginationStart = 0
numberOfImagesPerPage = 5
imageData.removeAllObjects()
var findImageData: PFQuery = PFQuery(className: "Image")
findImageData.whereKey("deleted", equalTo: 0)
findImageData.orderByDescending("createdAt")
findImageData.skip = forPaginationStart
findImageData.limit = numberOfImagesPerPage
findImageData.findObjectsInBackgroundWithBlock{
(objects:[AnyObject]?, error:NSError?)->Void in
if error == nil{
for object in objects! {
let image:PFObject = object as! PFObject
self.imageData.addObject(image)
//comment counts
var countComments = PFQuery(className:"Comment_image")
countComments.whereKey("image_id", equalTo: PFObject(withoutDataWithClassName: "Image", objectId: "\(image.objectId!)"))
countComments.whereKey("deleted", equalTo: 0)
countComments.countObjectsInBackgroundWithBlock {
(count: Int32, error: NSError?) -> Void in
if error == nil {
println("There is \(count) comments for image: \((object.objectId!)!)")
self.commentCount.append(countComments.countObjects())
}
}
}
self.tableView.reloadData()
}
}
//count number of records in DB for pagination end
var totalNumOfRows: PFQuery = PFQuery(className: "Image")
totalNumOfRows.whereKey("deleted", equalTo: 0)
totalNumOfRows.countObjectsInBackgroundWithBlock {
(count: Int32, error: NSError?) -> Void in
if (error == nil) {
self.totalNumRecords = Int(count)
println("total records: \(self.totalNumRecords)")
}
}
}
I had it working with php and SQL but with parse the same approach doesnt work for me... I asked the DB what is the value for that user, if it was 1 the user upvoted and if negative one he downvoted, 0 neither one and I set the button according to the value
SQL query:
IFNULL((SELECT value FROM rating WHERE image.id = rating.image_id AND rating.user_id = (SELECT id FROM user WHERE uuid='$uuid')), 0) as UserRating
- (instancetype)whereKey:(NSString *)key matchesKey:(NSString *)otherKey inQuery:(PFQuery *)query
You can use this method on PFQuery, for example:
var countComments = PFQuery(className:"Comment_image")
var findImageData: PFQuery = PFQuery(className: "Image")
findImageData.whereKey("deleted", equalTo: 0)
findImageData.orderByDescending("createdAt")
findImageData.skip = forPaginationStart
findImageData.limit = numberOfImagesPerPage
countComments.whereKey("image_id", matchesKey:"objectId", inQuery: )
countComments.whereKey("deleted", equalTo: 0)
countComments.countObjectsInBackgroundWithBlock {
(count: Int32, error: NSError?) -> Void in
if error == nil {
println("There is \(count) comments for all images!")
}
}
By setting up the count query like this, you don't have to call count for each image, but I don't know in the background if Parse optimizes to make it faster than your current query.

Use multiple contains functions in if statement (Swift)

Is there a way to use multiple "contains(array, value)" functions in an if statement? I have a query that stores in the results in an array. If the results are nil I perform a set of operations. If, however, the array is not nil I'd like to check to see if certain objects appear in it:
var user: PFUser?
override func viewDidLoad() {
super.viewDidLoad()
//get the two players
if let user = user{
var userQuery = PFQuery(className: "Game")
userQuery.whereKey("user1", equalTo: user)
userQuery.whereKey("isActive", equalTo: true)
var userQuery2 = PFQuery(className: "Game")
userQuery2.whereKey("user2", equalTo: user)
userQuery2.whereKey("isActive", equalTo: true)
var currentUserQuery = PFQuery(className: "Game")
currentUserQuery.whereKey("user1", equalTo: PFUser.currentUser())
currentUserQuery.whereKey("isActive", equalTo: true)
var currentUserQuery2 = PFQuery(className: "Game")
currentUserQuery2.whereKey("user2", equalTo: PFUser.currentUser())
currentUserQuery2.whereKey("isActive", equalTo: true)
var query = PFQuery.orQueryWithSubqueries([userQuery, userQuery2, currentUserQuery, currentUserQuery2])
query.findObjectsInBackgroundWithBlock{
(results: [AnyObject]!, error: NSError!) -> Void in
if error == nil{
//if there are no active games, start new game
if results == nil{
//start game code
} else if contains(results, user) as [AnyObject]! && contains(results, PFUser.currentUser()) as [AnyObject]! {
println(results)
}
}
}
}
}
Using the && operator is returning an error on the line: Cannot invoke '&&' with an argument of type '([AnyObject]!,[AnyObject]!)'. Any ideas on how I can test to see if the user and PFUser.current_user() objects are found in the array?
Thanks!
Problem is that you are trying to cast result of contains func call which is Bool in to array of AnyObjects's.
What you need to do instead is just to compare results of contains(results, user) and contains(results, PFUser.currentUser()) functions calls. But it wont compile.
In order to make your compiler happy you need to cast results array to array of [PFUser]
See code:
if let users = results as? [PFUser] {
if contains(users, user) && contains(users, PFUser.currentUser()) {
println(users)
}
}

Parse Query Profile Data for User Profile - Swift

I want to query the user data based on the profile you are on in my app. As of now my query just gets all the posts not just the user that the profile belongs too.
"Drives" is the class name of the user posts.
post.removeAll(keepCapacity: false)
var findTimelineData:PFQuery = PFQuery(className:"Drives")
findTimelineData.findObjectsInBackgroundWithBlock
{
(objects:[AnyObject]! , error:NSError!) -> Void in
if error == nil
{
self.post = objects.reverse() as [PFObject]
self.table.reloadData()
}
}
post.removeAll(keepCapacity: false)
var findTimelineData:PFQuery = PFQuery(className:"Drives")
//Add the next line
findTimelineData.whereKey("YOUR_COLUMN_NAME_WHERE_THE_USERS_ARE_STORED", equalTo: "THE_NAME_OF_THE_USER")
findTimelineData.findObjectsInBackgroundWithBlock
{
(objects:[AnyObject]! , error:NSError!) -> Void in
if error == nil
{
self.post = objects.reverse() as [PFObject]
self.table.reloadData()
}
}
Or instead you can choose any whereKey... function, listed as here: https://parse.com/docs/ios/api/Classes/PFQuery.html#//api/name/whereKey:equalTo:
UPDATED:
If you query a pointer field, then the whereKey is modified a bit, you have to use relational queries:
let userNameQuery = PFQuery(className: "THE_CLASSNAME_WHERE_THE_USERS_ARE_STORED")
userNameQuery.whereKey("YOUR_COLUMN_NAME_WHERE_THE_NAME_OF_THE_USERS_ARE_STORED", equalTo: "THE_NAME_OF_THE_USER")
let findTimelineData:PFQuery = PFQuery(className:"Drives")
findTimelineData.whereKey("POINTER_COLUMN_OF_USER", matchesQuery: userNameQuery)

Resources