Parse display images in collectionView - swift

I have a tableView with a CollectionView inside, in the TableView is the users details and I am trying to put the images belonging to the user in the collectionView.
So far I have everything working correctly apart from the images are not correct. Every image uploaded is displaying for each user, I need match the images to the user...I have been trying for a while now and have not found a solution so I'm hoping someone can help me here!
Here is my query for getting the user details and images:
func loadPosts() {
let followQuery = PFQuery(className: "Follows")
followQuery.whereKey("follower", equalTo: PFUser.currentUser()!.username!)
followQuery.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in
if error == nil {
self.followArray.removeAll(keepCapacity: false)
for object in objects! {
self.followArray.append(object.valueForKey("following") as! String)
}
self.followArray.append(PFUser.currentUser()!.username!)
let query = PFQuery(className: "Posts")
query.whereKey("usernamee", containedIn: self.followArray)
query.addDescendingOrder("createdAt")
query.findObjectsInBackgroundWithBlock({ (objects:[PFObject]?, error:NSError?) -> Void in
if error == nil {
self.usernameArray.removeAll(keepCapacity: false)
self.imageArray.removeAll(keepCapacity: false)
self.uuidArray.removeAll(keepCapacity: false)
for object in objects! {
self.usernameArray.append(object.valueForKey("username") as! String)
self.imageArray.append(object.valueForKey("imageArray") as! PFFile)
self.uuidArray.append(object.valueForKey("uuid") as! String)
}
self.tableView.reloadData()
} else {
print(error!.localizedDescription)
}
})
} else {
print(error!.localizedDescription)
}
})
}
Best regards.

There are three issues I see in your code. First of all, in your Posts query, you use the whereKey() method: query.whereKey("usernamee", containedIn: self.followArray). username is spelled incorrectly, and this may be causing some issues. In addition, you remove all objects from your array every time you issue a query. There must be some way to add and remove data as necessary. (This only applies if you are refreshing data. If you have not added data to the arrays, there is no need to empty them.) Finally, you have a value called imageArray that is stored as a PFFile. An array would be multiple objects. It is hard to tell whether or not each user has multiple images or not. (For the solution, I assume that there is only one image per user. I would suggest changing the name of the key to be more precise. If this is not the case, write me a comment.)
Now, in regards to your actual problem, I believe that this is because you have only one array for all the images, and when you display it for each user, the entire array is used. In order to correct for this, I would suggest building a data structure in the following format: [(String, Image)], where String is the username and Image is file for the image. You can thus access the users and their images as an array of tuples.
To create the array of tuples, I would suggest: var usersAndImagesArray = [(username: String, imageFile: PFFile)]()
To add to the array:
for object in objects!{
usersAndImagesArray.append(object.valueForKey("username") as! String, object.valueForKey("image") as! PFFile)
}
To access the values when setting up the cells:
let username = usersAndImagesArray[index.row].username
let imageFile = usersAndImagesArray[index.row].imageFile

Related

Is it possible to create an Array of specific Objects in Parse?

I have made a QR scanner App, I have manually put some QR codes into parse for it to recognise, any QR codes scanned that I haven't put into parse don't get recognised.
The only thing to tell them apart is their (Info) i.e "restaurant", "nail salon" etc.
I am after a way to be able to record an Integer of how many times the chosen QRCode has been scanned, to then place on a label in the app.
I can (.count) ALL of the qrCodes saved and scanned by the user but can't seem to figure out how I can then either put all "Nail Salons" into their own array on parse or run a For loop matching the ones I need.
// The code below will retrieve everything in the "info" column and print it to console
// This prints "Nails Salon" x 5, "Restaurant" x3 and "Coffee Shop" x 7 in the order that they were scanned (Unorganised)
// What block of code could I make to display what PFuser.current currently has in their parse?
// E.g. PFUser has scanned "Nail Salon" 5 Times, "Restaurant" 3 time etc etc
let infoCheck = PFQuery(className: "UserQRCodes")
infoCheck.whereKey("info", contains: "")
infoCheck.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
if let error = error {
print(error.localizedDescription)
} else if let objects = objects {
print(objects)
}
}
// To retrieve everything the USER has scanned and display it as String on the APP
let query = PFQuery(className: "UserQRCodes")
query.whereKey("userName", equalTo: PFUser.current()!)
query.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
if let error = error {
//log details of the failure
print(error.localizedDescription)
} else if let objects = objects {
let stampees: Int = objects.count
let totalStampees = String(stampees)
self.stampeesCollectedLabel.text = totalStampees
print(objects.count)
}
}
// Do any additional setup after loading the view.
}
You want to filter elements in your array of scans. For each code type, call something like
// '$0' is your PFObject. Replace 'name' with whatever `PFObject` property
// represents the object's type
let nailSalons = objects.filter { $0.name == "Nail Salon" }
You can then use this filtered array to get your count.
Note that the filter { $0... } syntax is a shorthand for
objects.filter { (object) throws -> Bool) in
return object.name == "Nail Salon"
}
You'll need to use the full version if your condition is anything more complicated than a simple one-line expression. Note that in the short version, the return is implied.

Difference between generate and append when querying and adding data to array?

var objectarray = [PFObject]()
func populateTable() {
query.findObjectsInBackgroundWithBlock { (objects, error) in
self.objectarray.removeAll(keepCapacity: true)
self.searchTableView.reloadData()
if error == nil {
Above is the query I am doing and the below 2 codes are what I can use to use the query to populate a array.
if let objects = objects as [PFObject]! {
self.objectarray = Array(objects.generate())
}
Is there any difference with running this code above to populate my array or running the code below?
for object in objects! {
self.objectarray.append(object)
}
Doing either works to load onto my tableView. Also another question regarding Parse. After doing the above, the user doesn't download PFFiles from the background until I run
getDataInBackgroundWithBlock
right? I want to know if it'd be beneficial to save smaller versions of images onto the server.

How can I access value from pointer inside pointer on query with Parse in swift?

How can I access value from pointer inside pointer on query with Parse in swift?
This has to do with three classes in Parse. One is called Post_Story, on is Post and one is User. When I query for Post_Story i am able to retreive pointer info from a key "Post", but is there any way to access information from a pointer within that pointer? (key "createdBy" to User class)
My code looks like this:
let postQuery = PFQuery(className: "Post_Story")
postQuery.whereKey("story", containedIn: storyObjects)
postQuery.includeKey("post")
postQuery.findObjectsInBackgroundWithBlock { (objects:[PFObject]?, error:NSError?) in
if error == nil {
for object in objects! {
if let postL = object["post"] as? PFObject {
if postL["createdBy"] != nil {
//this is where I want to get infor from PFUser, but not all info is sent
print((postL["createdBy"] as! PFUser)) //prints PFUser object without username etc.
}
}
}
}
else {}
}
I hope the question is not too stupid...
You can add postQuery.includeKey("post.createdBy") to include the user object in the createdBy column of post.

swift parse query not returning objects in ascending order

My query aims to fetch the objects created by a certain number of users. The following are my codes:
var dates = [NSDate]()
var messages = [String]()
let getUsersQuery = PFQuery(className: "followings")
getUsersQuery.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
getFollowedUsersQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
for object in objects {
let followedUser = object["followedUser"] as! String
let getPostsQuery = PFQuery(className: "posts")
getPostsQuery.whereKey("userId", equalTo: followedUser)
getPostsQuery.orderByDescending("updatedAt")
getPostsQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for post in objects {
self.dates.append(post.createdAt as NSDate!)
self.messages.append(post["message"] as! String)
}
print(self.dates)
self.myTableView.reloadData()
})
}
}
}
})
Here's what come up when I print the dates in the log:
However, the content in my tableview is not in an ascending order. In fact the post created most recently is at the bottom of the tableview. One thing I noticed is that the posts of a particular user seems to be shown together. So all of one user's post would appear at the top of the tableview no matter another user has created the newest post. Any idea what is going wrong? thanks!
What you need is a nested query. The array you are trying to get is an of posts. So your query should be on the post table. (Also, your table should be called post not posts. Table names should always be singular. Each post is called a post not a posts.
let postsQuery = PFQuery(className: "post")
postsQuery.orderByDescending("updatedAt")
let usersQuery = PFQuery(className: "following")
usersQuery.whereKey("follower", equalTo:PFUser.currentUser()!.objectId!)
// this might need to change depending on what the followed user is called in the `followings` table.
postsQuery.whereKey("userId", matchesKey:"followed", inQuery:usersQuery)
getPostsQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for post in objects {
self.dates.append(post.createdAt as NSDate!)
self.messages.append(post["message"] as! String)
}
print(self.dates)
self.myTableView.reloadData()
})
Also, you are saving dates and messages in separate arrays. Create a struct or something that has a date and string and have a single array of Post objects.

How do you store a dictionary on Parse using swift?

I am very new to swift and I don't know Obj C at all so many of the resources are hard to understand. Basically I'm trying to populate the dictionary with PFUsers from my query and then set PFUser["friends"] to this dictionary. Simply put I want a friends list in my PFUser class, where each friend is a PFUser and a string.
Thanks!
var user = PFUser()
var friendsPFUser:[PFUser] = []
var friendListDict: [PFUser:String] = Dictionary()
var query = PFUser.query()
query!.findObjectsInBackgroundWithBlock {
(users: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(users!.count) users.")
// Do something with the found objects
if let users = users as? [PFUser] {
friendsPFUser = users
for user in friendsPFUser{
friendListDict[user] = "confirmed"
}
user["friends"] = friendListDict //this line breaks things
user.saveInBackground()
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
To be clear, this code compiles but when I add
user["friends"] = friendListDict
my app crashes.
For those who might have this issues with. "NSInternalInconsistencyException" with reason "PFObject contains container item that isn't cached."
Adding Objects to a user (such as arrays or dictionaries) for security reasons on Parse, the user for such field that will be modified must be the current user.
Try signing up and using addObject inside the block and don't forget do save it!
It helped for a similar problem I had.