【发布时间】:2015-03-29 23:35:18
【问题描述】:
我有一种方法可以加载用于用户个人资料库的图像。使用我的数据库中的“updatedPhotoAt”列按降序加载图像。
“updatedPhotoAt”列的目的是允许用户设置默认图像。此列仅在用户滑动图像并点击“默认”按钮时才会更新。 “updatedPhotoAt”列中日期最近的图片为默认图片。
这是我的代码:
func loadPhotoData() {
pageViews = []
pageImages = []
let query = PFQuery(className: "UserImages")
query.whereKey("user", equalTo: PFUser.currentUser())
query.addDescendingOrder("updatedPhotoAt")
query.findObjectsInBackgroundWithBlock { (objects: Array!, error: NSError!) -> Void in
if error != nil {
println("0 images found")
} else {
for object in objects as Array {
var imgDate = object["updatedPhotoAt"] as NSDate
println("1: \(imgDate)")
var imageFile = object["image"] as PFFile
var imgView = PFImageView()
imgView.file = imageFile
imgView.loadInBackground({ (image: UIImage!, error: NSError!) -> Void in
if error != nil {
//error alert
} else {
self.pageImages.append(image)
var imgDate = object["updatedPhotoAt"] as NSDate
println("2: \(imgDate)")
正如您在查询数据库时所看到的,我确保使用“updatedPhotoAt”列以降序返回结果。在我的块中,我打印了这些列的日期值,以便查看事情是否正常。
最初是:
1: 2015-01-30 12:15:54 +0000
1: 2015-01-22 10:23:00 +0000
1: 2015-01-14 10:19:00 +0000
但是在 imgView 块中他们失去了顺序:
2: 2015-01-22 10:23:00 +0000
2: 2015-01-14 10:19:00 +0000
2: 2015-01-30 12:15:54 +0000
这意味着我的默认图像只是偶然显示,并且图像始终以随机顺序显示在用户照片库中(图库是一个滚动视图,允许用户水平滚动图像。默认图像应始终在列表)。
我发现问题是由 imgView 块引起的。有什么办法可以保持图片的顺序吗?
【问题讨论】:
-
问题是
loadInBackground,图像的接收顺序与请求的顺序不同。这是意料之中的,您必须围绕它进行编程。 -
我建议按原样插入。然后,当您想显示该数组的内容时,只需使用正确的谓词对其进行排序。
标签: ios cocoa-touch swift uiimageview objective-c-blocks