Reputation: 86
I am writing an application in Swift that uses Parse for its backend. The user can post images to parse and they are displayed in a collectionView when the user logs in.
The new problem I am having is when retrieving all of my images from parse for the current user, the images are displayed all out of order. Actually, just about every time the images are retrieved they are in a slightly different order. I tried looking at other posts where people had this problem but they didn't seem to do the trick.
Here is my code to retrieve all image posts by a user (the current user):
func retrieveAllImagesForUserId(userId:String){
self.arrayOfUserPosts.removeAll()
let query = PFQuery(className: "ImagePost")
query.whereKey("UserId", equalTo: userId)
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock {
(object:[PFObject]?, error:NSError?) -> Void in
if ( error != nil ){
print(error?.localizedDescription, error?.userInfo)
} else {
for temp: PFObject in object! {
let username: String = temp["Username"] as! String
let userId: String = temp["UserId"] as! String
let description: String = temp["ImageDescription"] as! String
let imageId: String = temp.objectId!
let file: PFFile = temp["Image"] as! PFFile
file.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
let retrievedImage = UIImage(data: imageData)
self.imagePost = ImagePost.init(
image: retrievedImage!,
userId: userId,
imageId: imageId,
description: description,
username: username)
self.arrayOfUserPosts.append(self.imagePost!)
NSNotificationCenter.defaultCenter().postNotificationName("retrievedPost", object: nil)
}
}
}
}
}
}
}
The first thing i did was remove all objects to avoid any possible duplications to occur, maybe from a previous user who was logged on to the same device or anything like that.
For every image posted, i am also posting a column which has the users objectId whom posted that image. This way, i can query where said key is equal to the current users objectId.
I am doing an additional query to orderByDescending("createdAt") so the new images will be displayed at the top of my collection view.
My ImagePost class is an extremely simple class that is used for populating the area with objects, it looks like this:
class ImagePost { var postedImage: UIImage? var userId: String? var imageId: String? var description: String? var username: String?
init(image:UIImage, userId:String, imageId:String, description: String, username: String ) {
self.postedImage = image
self.userId = userId
self.imageId = imageId
self.description = description
self.username = username
}
}
When the data is retrieved, i append the new ImagePost object to my array, which is the array i used to populate my collectionView and send a notification to reload the collection view.
I just really don't understand why I am having this problem so all of a sudden where the images are being retrieved in almost any order they choose. Any help would be greatly appreciated.
If you know the solution in objective-c but not swift that will also be helpful.
Thank you, Rob
Upvotes: 0
Views: 257
Reputation: 759
I had integrated something similar using Parse.
You don't need to fetch all the images at first. You can use a third party library SDWebImageCache
for downloading the image when needed or caching.
Have the postedImage
type as PFFile
and assign the imageFile directly. No need of fetching the imageData.
Have another key called updatedAt
in ImagePost
class. No need of using predicate when querying the objects from Parse
. Save the updatedAt
time of ImagePost
class. So now you can directly append the data to arrayOfUserPosts
.
After completion of the loop, you can sort the array and assign it to self.arrayOfUserPosts
.
Then in tableView's dataSource tableView:cellForRowAtIndexPath:
method, you can do something like,
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:file.url] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
// Any custom view initialisation if needed.
}
where imageView is your display view for image, file is the object of type PFFile which represent the image. This is a Objective-C code. Similar syntax for Swift.
Upvotes: 1