如何在将大量数据插入核心数据时删除recive内存警告问题?

时间:2016-10-26 10:09:17

标签: ios objective-c swift database core-data

我正在使用此功能进行插入,但我正面临收到内存Waring问题。我要将图库的图像路径的本地标识符保存到核心数据中。我已经通过功能描述了如何从iPhone库中获取所有图像并将其保存到核心数据中。请给我有用的建议。

func getAllImageFromIphoneGallery() {

let allPhotosOptions = PHFetchOptions()
allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
self.assetsFetchResult  = PHAsset.fetchAssetsWithMediaType(.Image, options: allPhotosOptions)
let manager = PHImageManager.defaultManager()
self.assetsFetchResult.enumerateObjectsUsingBlock{(object: AnyObject!,
   count: Int,
     stop: UnsafeMutablePointer<ObjCBool>) in

      if object is PHAsset{
       let asset = object as! PHAsset
       let imageSize = CGSize(width: asset.pixelWidth,
       height: asset.pixelHeight)
      /* For faster performance, and maybe degraded image */
       let options = PHImageRequestOptions()
       options.deliveryMode = .FastFormat
       options.synchronous = true
       manager.requestImageForAsset(asset,
       targetSize: imageSize,
       contentMode: .AspectFill,
       options: options,
       resultHandler: {
       (image, info) -> Void in
       debugPrint(object.localIdentifier)

       if   let imageURL : NSURL? = info!["PHImageFileURLKey"] as AnyObject? as? NSURL
       {
        if imageURL != nil{
        //  let urlString: String = imageURL!.path!
        let urlString: String = object.localIdentifier


         print("this is image path \(urlString)")
         self.arrPhotoPath.addObject(urlString)

         }
       }
     }           
    )
  }
 }
}


func createTask() {

    let context = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)
    context.persistentStoreCoordinator = (UIApplication.sharedApplication().delegate as! AppDelegate).persistentStoreCoordinator

    context.performBlock {
        while(true) { //
            autoreleasepool {

                let entityDescripition = NSEntityDescription.entityForName("PhotoLibrary", inManagedObjectContext: self.managedObjectContext)
                debugPrint(self.arrPhotoPath);
                let array = self.arrPhotoPath
                if (array.count > 0)
                {
                    for item in array {
                        let task = Tasks(entity: entityDescripition!, insertIntoManagedObjectContext: self.managedObjectContext)
                        task.frameid = "1"
                        task.photopath = item as! String
                        task.accesstoken = ""
                        task.photoname = "abc"
                        task.deliverystatus = "no"
                        array.removeObject(item)
                    }
                }

            }
            do {
                try self.managedObjectContext.save()
            } catch _ {
            }
            self.managedObjectContext.reset()
        }
    }
}

0 个答案:

没有答案