我试图将队列与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()
})
}
}
答案 0 :(得分:0)
将保存到coredata的代码放入主队列