核心数据图像不持久吗?

时间:2018-12-27 20:02:08

标签: ios swift core-data

我正在使用此功能在视图控制器中存储和创建帖子:

@objc func createPost(){
        guard let appDelegate =
        UIApplication.shared.delegate as? AppDelegate else {
            return
        }
        let managedContext =
            appDelegate.persistentContainer.viewContext
        let post = Post(context: managedContext)

        var mediaURI: URL?
        let timeInSeconds = Int64((postDate?.timeIntervalSince1970)!)
        if isVideo == true {
            let filename = String(timeInSeconds) + ".MOV"
            FileService.uploadVideo(videoURL: pickedVideoURL, name: filename)
            post.mediaFilename = filename
        } else {
            let filename = String(timeInSeconds) + ".JPG"
            FileService.uploadImage(image: postImage, name: filename)
            post.mediaFilename = filename
        }
        var postTags:[Tag] = []

        if let tokens = tagsView.tokens() {
            for token in tokens {
                let tagFetchRequest: NSFetchRequest<Tag> = Tag.fetchRequest()
                tagFetchRequest.predicate = NSPredicate(format: "name == %@", token.title)
                do {
                    let res = try managedContext.fetch(tagFetchRequest)
                    var tag: Tag!
                    if res.count > 0 {
                        tag = res.first
                    } else {
                        tag = Tag(context: managedContext)
                        tag.name = token.title
                        tag.mostRecentUpdate = NSDate()
                        tag.mostRecentThumbnail = postImage?.jpegData(compressionQuality: 1.0) as NSData?
                    }
                    postTags.append(tag)
                } catch let error as NSError {
                    print("Could not fetch. \(error), \(error.userInfo)")
                    return
                }
            }
        }
        for tag in postTags {
            post.addToTags(tag)
        }
        post.isVideo = isVideo!
        post.thumbnail = pickedPostThumbnail?.jpegData(compressionQuality: 1.0) as NSData?
        post.notes = notesView.text
        post.timeStamp = postDate! as NSDate
        do {
            try managedContext.save()
            dismiss(animated: true, completion: nil)
        } catch let error as NSError {
            print("Could not save. \(error), \(error.userInfo)")
        }
    }

我在那里做很多事情,但值得注意的是,我正在将UIImage设置为NSData的post.thumbnail?最后,我要保存上下文。

因此,我创建了帖子并关闭了应用程序,然后再次将其打开,另一个视图控制器执行了此操作:

 fileprivate lazy var posts: NSFetchedResultsController<Post> = {
    let appDelegate =
        UIApplication.shared.delegate as? AppDelegate
    let managedContext =
        appDelegate?.persistentContainer.viewContext
    let request: NSFetchRequest<Post> = NSFetchRequest(entityName: "Post")
    request.predicate = NSPredicate(format: "%@ IN self.tags", tag)
    let timeSort = NSSortDescriptor(key: "timeStamp", ascending: true)
    request.sortDescriptors = [timeSort]

    let posts = NSFetchedResultsController(fetchRequest: request, managedObjectContext: managedContext!, sectionNameKeyPath: nil, cacheName: nil)
    posts.delegate = self
    return posts
}()

override func viewDidLoad(){ 
....
do {
        try posts.performFetch()

} catch let error as NSError {
       print("Could not fetch. \(error), \(error.userInfo)")
}
...
}

let post = posts.object(at: indexPath)
cell.mediaFileName = post.mediaFilename
if let data = post.thumbnail {
        print(data,"data?")
        cell.thumbnail.image = UIImage.init(data: data as Data)
    } else {
        print("couldn't find")
    }

前几次重新启动后,此方法工作正常。但是很快,它开始打印(“找不到”)并且不加载图像。与发布相关的其他字段仍在加载中。这是在Xcode随附的iPhone 8模拟器上运行的。

我在这里做错了什么?关于模拟器如何将图像存储在不持久的核心数据中,是否有什么问题?还是我滥用了托管上下文?

0 个答案:

没有答案