0

I am making this marketplace and every time i fetch user post on the timeline i get this on the home page: Warning: A long-running operation is being executed on the main thread. I have this code in my post Class (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!)
        }

    }

and this is how i load my post in the timeline :

 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()
    }

also i have a parse helper class :

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)



        }


}
Yahyacode
  • 47
  • 1
  • 7
  • In case you're wondering, the problem is dispatch_async(dispatch_get_main_queue()). The main dispatch queue is a globally available serial queue that executes tasks on the application’s main thread. – Russell Sep 03 '15 at 04:55
  • @matt from what i know By calling the saveInBackgroundWithBlock method, uploading the data happens on a background thread, and our app no longer freezes. Can you explain more what do you mean by the code is not executed in the order written and how that can show a Warning: A long-running operation . – Yahyacode Sep 03 '15 at 12:57
  • @matt The saveInBackgroundWithBlock method allows us to pass it a callback in the form of a closure . The code in that callback is executed once the task running on the background thread is completed, e.g. when the photo upload is completed. – Yahyacode Sep 03 '15 at 12:59
  • @Russell i tried to remove the dispatch_async(dispatch_get_main_queue()). but i still get the same error, what do you think ? – Yahyacode Sep 03 '15 at 12:59
  • @matt what do you think ? – Yahyacode Sep 06 '15 at 17:44

0 Answers0