2014-12-27 4 views
4

ProfileViewController에는 PF 파일로 저장된 사용자 프로필 그림을 검색하는 쿼리가 있습니다.장기 실행 작업이 주 스레드에서 실행 중입니다. Swift

var query = PFQuery(className:"Users") 
    query.whereKeyExists("profilePicture") 
    query.findObjectsInBackgroundWithBlock { 
     (objects: [AnyObject]!, error: NSError!) -> Void in 
     if error == nil { 

      self.userNameLabel.text = PFUser.currentUser().username 

      if let imageFile = PFUser.currentUser().objectForKey("profilePicture") as? PFFile { 
       if let data = imageFile.getData() { 
        self.profPic.image = UIImage(data: data) 
       } 
      } 

     } 
     else { 
      println("User has not profile picture") 
     } 
    } 

이보기에서 유일한 쿼리이며 모든 사용자의 모든 게시물이있는 내 응용 프로그램의 홈 페이지에 다른 쿼리가 있습니다. 내가 받고있는 오류 A long-running operation is being executed on the main thread. 뒤에 오는 사람 Break on warnBlockingOperationOnMainThread() to debug.

내가 프로필을 위해 현재의 사용자 지위를 얻기 위해 또 다른 쿼리를해야하기 때문에 이것을 특히 어떻게해야할지 모르겠다. findObjectsInBackgroundWithBlock 이외의 것을 사용해야합니까? 감사.

+0

이것이 문제의 근원임을 절대적으로 긍정적입니까? '... WithContentsOfURL' 또는'sendSynchronousRequest'에 대해 느껴지지 않습니다. 메인 스레드를 차단하고있는 것은 없습니까? BTW는 인스 트루먼 트의 이러한 종류의 문제를 식별하는 데 도움이되는 "Record Waiting Threads"기능을 가지고 있습니다. 그거 사용해 봤어? – Rob

+0

WithContentsOfUrl 또는 sendSynchronousRequest 중 하나를 사용하지 않았으므로이 Instruments 기능을 설명해 주셔서 감사합니다. @Rob – kareem

+0

WWDC 2014 앱의 "시간 프로파일 링"섹션을 참조하십시오. [계측기로 어플리케이션 향상] (https://developer.apple .com/videos/wwdc/2014 /? id = 418), 동영상에 약 18 분. 이전에 다른 해의 비디오에서도 다루어졌지만, 시작하기에 좋은 장소입니다. 그러나 danh는 문제의 원인을 밝혀 냈으므로이 시점에서 Instruments는 아마도 필요하지 않을 것입니다. 그러나 다음 번에 Instruments를 사용하여 문제의 원인을 직접 찾을 수 있습니다. – Rob

답변

5

경고는 Parse SDK에서 가져온 것입니다. 이 부분은 imageFile.getData()이고, Parse는 차단 호출을 사용할 때 경고 할만큼 친절합니다. 대체품으로 사용할 수있는 두 종류의 getDataInBackground ...이 있습니다. See them in the docs here.

+0

어떤 방법을 권해 주시겠습니까? 그래서 findObjectsWithBlock을 사용해야하지 않습니까? @danh – kareem

+0

getDataInBackground가 작동 할 수 있습니다. 다음 다양성 ... WithBlock : 완료 시점을 알려줍니다. 또 다른 ... WithBlock : ProgressBlock : 완료되면 알려주고, 완료되면 완료율을 알려줍니다. (그러나 이것은 PFFile 클래스이므로 객체를 찾지 않고 데이터를 얻음을 기억하십시오). – danh

3

@danh 솔루션에 대해 자세히 설명하려면 업데이트 된 소스 코드이며 완벽하게 정상적으로 작동합니다. @danh!

override func viewDidLoad() { 
    super.viewDidLoad() 


    var query = PFQuery(className:"Users") 
    query.whereKeyExists("profilePicture") 
    query.findObjectsInBackgroundWithBlock { 
     (objects: [AnyObject]!, error: NSError!) -> Void in 
     if error == nil { 

      self.userNameLabel.text = PFUser.currentUser().username 

      if let imageFile = PFUser.currentUser().objectForKey("profilePicture") as? PFFile { 
      imageFile.getDataInBackgroundWithBlock { (data: NSData!, error: NSError!) -> Void in 
        self.profPic.image = UIImage(data: data) 
       } 
      } 

     } 
     else { 
      println("User has not profile picture") 
     } 
     } 
    } 
관련 문제