swift - 解决 iPhone 处理器过载时的队列问题

标签 swift 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()
        })
      }
  }

最佳答案

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

关于swift - 解决 iPhone 处理器过载时的队列问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34568986/

相关文章:

ios - NSPredicate 用于检测属性的类

ios - 查看Device中的核心数据表

ios - 从 UITextField 继承的自定义类在 Swift 中不起作用(使用自定义 init)

ios - iOS 中的不同背景模式

swift NSCoding 不工作

ios - 如何解决核心数据错误 : Attempt to add read-only file at path when trying to access Core Data container in the app extension today widget?

ios - Framework ViewController View 未显示

ios - 如何合并 Framework SwiftUI

swift - 如何在 NSManagedObject Swift 扩展中创建托管对象子类的实例?

ios - 核心数据更新记录改为插入