我正在创建这个市场,每次我在时间线上获取用户帖子时都会在主页上显示:警告:正在主线程上执行长时间运行的操作。 我在我的帖子类(PFObject,PFSubclassing)中有这个代码:
func downloadImage() {
image1.value = Post.imageCache[self.imageFile!.name]
image2.value = Post.imageCache[self.imageFile2!.name]
image3.value = Post.imageCache[self.imageFile3!.name]
if (image1.value == nil) {
imageFile?.getDataInBackgroundWithBlock { (data: NSData?, error: NSError?) -> Void in
if let data = data {
let image1 = UIImage(data: data, scale:1.0)!
self.image1.value = image1
Post.imageCache[self.imageFile!.name] = image1
}
}
}
if (image2.value == nil) {
imageFile2?.getDataInBackgroundWithBlock { (data: NSData?, error: NSError?) -> Void in
if let data = data {
let image2 = UIImage(data: data, scale:1.0)!
self.image2.value = image2
Post.imageCache[self.imageFile2!.name] = image2
}
}
}
if (image3.value == nil) {
imageFile3?.getDataInBackgroundWithBlock { (data: NSData?, error: NSError?) -> Void in
if let data = data {
let image3 = UIImage(data: data, scale:1.0)!
self.image3.value = image3
Post.imageCache[self.imageFile3!.name] = image3
}
}
}
}
func uploadPost() {
user = PFUser.currentUser()
let ACL = PFACL(user: user!)
ACL.setPublicReadAccess(true)
self.ACL = ACL
let location = PFGeoPoint?(postlocation!)
let Userlocation = PFGeoPoint?(location!)
let text = NSString?(title)
let Title = NSString?(text!)
let description = NSString?(productdescription!)
let Productdescription = NSString?(description!)
let tags = NSString?(tag!)
let itemtag = NSString?(tags!)
let period = NSString?(periode!)
let periodfield = NSString?(period!)
let price = NSNumber?(enteredprice!)
let Enteredprice = NSNumber?(price!)
let imageData = UIImageJPEGRepresentation(image1.value, 0.8)
let imageData2 = UIImageJPEGRepresentation(image2.value, 0.8)
let imageData3 = UIImageJPEGRepresentation(image3.value, 0.8)
let imageFile = PFFile(data: imageData)
let imageFile2 = PFFile(data: imageData2)
let imageFile3 = PFFile(data: imageData3)
user = PFUser.currentUser()
self.postlocation = Userlocation!
self.periode = periodfield!
self.title = Title!
self.enteredprice = Enteredprice!
self.productdescription = Productdescription
self.imageFile = imageFile
self.imageFile2 = imageFile2
self.imageFile3 = imageFile3
self.tag = itemtag
photoUploadTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in
UIApplication.sharedApplication().endBackgroundTask(self.photoUploadTask!)
}
saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
if let error = error {
ErrorHandling.defaultErrorHandler(error)
}
self.saveInBackgroundWithBlock(nil)
UIApplication.sharedApplication().endBackgroundTask(self.photoUploadTask!)
}
}
这就是我在时间轴中加载帖子的方式:
func loadInRange(range: Range<Int>, completionBlock: ([Post]?) -> Void) {
dispatch_async(dispatch_get_main_queue()) { () -> Void in
if let userlocation = self.userlocation {
ParseHelper.timelineRequestforCurrentLocation(range, location: self.userlocation!) { (result: [AnyObject]?, error: NSError?) -> Void in
//println("user have location")
self.posts = result as? [Post] ?? []
completionBlock(self.posts)
}
}
else {
//println("no user location")
}
}
activityIndicator.hidden = true
activityIndicator.stopAnimating()
}
我也有一个解析助手类:
class ParseHelper {
static func timelineRequestforCurrentLocation(range: Range<Int>,location : PFGeoPoint,completionBlock: PFArrayResultBlock) {
// Create a query for post
var query = PFQuery(className:"Post")
// Interested in locations near user.
query.whereKey("postlocation", nearGeoPoint:location, withinMiles: 30)
// Limit what could be a lot of points.
// query.limit = 50
query.orderByDescending("createdAt")
query.includeKey("user")
query.skip = range.startIndex
query.limit = range.endIndex - range.startIndex
query.findObjectsInBackgroundWithBlock(completionBlock)
}
}