队列解析为iphone处理器过载

时间:2016-01-02 18:02:26

标签: swift core-data

我试图将队列与Core Data结合起来。执行该功能后,处理器上升至99%并保持不变。如果我不使用队列,则处理器不会进入超速状态并返回1%。为什么?队列有什么问题?

      func addGalleryPhotoToCoreDataFromImage(image: UIImage) 
  {
   dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { () -> Void in
        // create a thumbnail and a smaller photo

        let thumbnailImage = self.resizeImage(image, targetSize: CGSizeMake(167, 167))
       let screenSize : CGSize = UIScreen.mainScreen().bounds.size
       let bigImage = self.resizeImage(image, targetSize: CGSizeMake(screenSize.width, screenSize.height))

        // save thumbnail and photo to CoreData
       let newPhoto = NSEntityDescription.insertNewObjectForEntityForName("Photo", inManagedObjectContext: self.managedObjectContext!) as! Photo
       newPhoto.thumbnail = UIImageJPEGRepresentation(thumbnailImage, 0.1)
       newPhoto.photo = UIImageJPEGRepresentation(bigImage, 0.9)
       self.photoSet!.addObject(newPhoto)
        self.saveToCoreData()
       dispatch_async(dispatch_get_main_queue(), { () -> Void in
           self.refreshItemsInGalleryCollectionView()
        })
      }
  }

1 个答案:

答案 0 :(得分:0)

将保存到coredata的代码放入主队列