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)
Related
I am trying to create an array of strings for all the usernames using the following code and populate a TableViewController.
class TableViewController: UITableViewController {
var randomUser = [String]()
override func viewDidLoad() {
super.viewDidLoad()
var query: PFQuery = PFUser.query()!
query.findObjectsInBackgroundWithBlock {(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil{
if let objects = (objects as? [PFObject]!){
for object in objects{
self.randomUser.append(object.objectForKey("username") as! String)
print(object.objectForKey("username") as! String)
print(self.randomUser.count)
}
}
}
}
print(self.randomUser.count)
}
the output in the console:
0
username
1
username
2
username
3
But UItableview does not populate.. What could be causing this?
My guess is that query is delayed and view is created before it can return data. Thank you for any help!
Yes, you are right. You need to call self.tableView.reloadData() after you get the results of the query. Below is an example of where to call it.
private var usersArray = [PFUser]()
func fetchUsers() {
let userQuery: PFQuery = PFUser.query()!
userQuery.orderByAscending("username")
userQuery.whereKey("username", notEqualTo: (currentUser?.username)!)
userQuery.findObjectsInBackgroundWithBlock({
(users, error) -> Void in
if error == nil {
self.usersArray = users as! [PFUser]
self.tableView.reloadData()
} else {
print(error)
}
})
}
In this example, you can then access the username property by doing usersArray[i].username
i am trying to retrive a user's data to get the user info from the _User class using the object id. i used this :
var data:NSMutableArray = NSMutableArray()
func loadData() {
data.removeAllObjects()
var profileQuery:PFQuery = PFUser.query()!
profileQuery.getObjectInBackgroundWithId(userId, block: { (objects, error) -> Void in
if error == nil {
self.data.addObject(objects!)
}
})
println(userId) // this is the userId as String.
println(self.data) ********* // empty array.
}
i am getting an empty array data here.. I've tried this also but same thing's happening here too. :
var profileQuery:PFQuery = PFUser.query()!
profileQuery.whereKey("objectId", equalTo: userId)
profileQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
for object in objects {
self.data.addObject(object)
}
}
}
Remember that "findObjectsInBackgroundWithBlock" happens async! You need to put any logic pertaining to the data inside of the block.
var profileQuery:PFQuery = PFUser.query()!
profileQuery.getObjectInBackgroundWithId(userId, block: { (objects, error) -> Void in
if error == nil {
self.data.addObject(objects!)
println(self.data) //shouldn't be empty.
}
})
Putting aside why you're using an NSMutableArray for a singular PFUser object—you're basically expecting to see self.data populated in the wrong location. The user would be added to it (assuming a successful retrieval), inside the closure. So do something like:
var data:NSMutableArray = NSMutableArray()
func loadData() {
data.removeAllObjects()
var profileQuery:PFQuery = PFUser.query()!
profileQuery.getObjectInBackgroundWithId(userId, block: { (objects, error) -> Void in
if error == nil {
self.data.addObject(objects!)
println(self.data) //...shouldn't be empty here
} else {
println("Error retrieving user: \(error.description")
}
})
}
I have a class called Posts in which i've postedBy column where i am saving the PFUser.currentUser() (pointer). so i want to retrieve the username, profile picture and stuff from the _User class using postedBy in the Posts class. What is the shortest and efficient way to achieve this? i am not much familiar with relation queries.
I believe that instead of saving the user pointer, you should save the user's username then it comes easier for you to retrieve everything.
var query = PFQuery(className:"Posts")
var username = PFUser.currentUser()?.username
query.whereKey("username", equalTo: username!)
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
if error == nil
{
if let objects = objects as? [PFObject]
{
for one in objects {
var pictureImage = one["theFile"] as! PFFile
pictureImage.getDataInBackgroundWithBlock({ (dataToget:NSData?, error:NSError?) -> Void in
if error == nil {
if let Image = UIImage(data: dataToget!){
// then you have the image
// save the image to array
// reload the tableview
}
}
})
}
}
}
}
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.
I have an app like instagram where i have to make relationship between users.
For that i decided to use the join table method.
It'' s just a table named "Activity" where i created rows "fromUser", "toUser" and a row for the activity type "type" which in this case is set to "followingAction".
Here how i set up the table :
var activity = PFObject(className:"Activity")
let currentUser = PFUser.currentUser()
let follow : String = "followingAction"
activity.setObject(currentUser, forKey: "fromUser")
activity.setObject(user, forKey: "toUser") // user is a another PFUser in my app
activity.setObject(follow, forKey: "type")
activity.saveEventually()
Ok, now i want to fetch all users that i m currently following (currentUser) and display them in a tableView
Following the doc https://parse.com/docs/relations_guide#manytomany-jointables
i made this but it only give me an array of users which only contain the user objectId, i can't have the email, name which are set up in the regular User table :
func loadUser () {
followingUserList.removeAllObjects()
let findUserObjectId = PFQuery(className: "Activity")
findUserObjectId.whereKey("fromUser", equalTo: userPassed)
findUserObjectId.whereKey("type", equalTo: "followingAction")
findUserObjectId.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!, error:NSError!) -> Void in
if error == nil {
// The find succeeded.
println("succesfully loaded the fromUser in Activity class")
// Do something with the found objects
for object in objects {
let user : PFUser = object["toUser"] as PFUser
self.followingUserList.addObject(user)
println("User added to following user list : \(user)")
println("followingUserlist = \(self.followingUserList)")
self.tableView.reloadData()
} } else {
// Log details of the failure
println("error loadind user ")
println(error)
}
}
}
if i print the followingUserList which is the array where i put the fetched users, here is what i have :
followingUserlist = (
"<PFUser: 0x7d9b93c0, objectId: DQJihBpW5E, localId: (null)> {\n}"
)
When i make a regular query (PFUser.query()) for the regular User table, i have more detail :
ex :
<PFUser: 0x7db5af80, objectId: niwftRrB5x, localId: (null)> {
backgroundImage = "<PFFile: 0x7db5f080>";
email = "kiki#kiki.com";
emailVerified = 0;
profileImage = "<PFFile: 0x7db62910>";
username = kiki;
}
Here we can see i have the complete PFUser with it email, username etc etc.
Because the "fromRow" in my "Activity" table is a pointer to my regular User table, why the fetched result in my loadUser() method isn't complete ?
Ok i finally found the solution, i just used the getObjectInBackgroundWithId method like this :
func loadUser () {
followingUserList.removeAllObjects()
let findUserObjectId = PFQuery(className: "Activity")
findUserObjectId.whereKey("fromUser", equalTo: userPassed)
findUserObjectId.whereKey("type", equalTo: "followingAction")
findUserObjectId.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!, error:NSError!) -> Void in
if error == nil {
// The find succeeded.
println("succesfully loaded the fromUser in Activity class")
// Do something with the found objects
for object in objects {
let user : PFUser = object["toUser"] as PFUser
let queryUsers = PFUser.query()
queryUsers.getObjectInBackgroundWithId(user.objectId, block: { (userGet :PFObject!,error : NSError!) -> Void in
self.followingUserList.addObject(userGet)
self.tableView.reloadData()
})
} } else {
// Log details of the failure
println("error loadind user ")
println(error)
}
}
}