核心数据托管对象的持久性问题

时间:2019-01-08 19:53:17

标签: swift core-data global-variables

我要做的是,我从api调用了一个类别列表,并检查它们的版本号,如果它们与数据库中保存的类别不同,我呼吁下载一个充满新歌曲数据的json文件并将它们保存在需要更新的每个类别的核心数据数据库中。

从api中获取类别管理对象并将其过滤为仅需要更新的类别后,我将类别管理对象保存在全局数组中,以便以后可以从URLSessionDownloadDelegate的扩展名访问它下载完成后,我将类别数据本身更新为“版本号等”。 我的问题是当我调用数组以在其中打印类别时。它是data<fault>,但是当我从viewDidLoad本身打印全局变量时就可以了。

var updatedCategoriesData = [CategoryManagedObject]()

override func viewDidLoad() {
    super.viewDidLoad()
    rotated()
    activityIndic.center = view.center
    view.addSubview(activityIndic)
    loadCategories()
    Constants.networkManager.loadCategories(checkUpdates: true) { (categories, msg) in
        guard categories != nil else {return}
        guard self.languages != nil else {return}
        if categories!.count > 0 {
            self.updatedCategoriesData = categories!

            print(self.updatedCategoriesData) //DATA NOT FAULT
            self.setupDownload()

        } else {
            print("NO UPDATE")
        }
    }

}

extension CategoriesTableViewController: URLSessionDelegate, URLSessionDownloadDelegate {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    percentageLabel.text = "99%"
    for tasksInfo in downloadTaskList {
        if tasksInfo.downloadTaskId == downloadTask.taskIdentifier {
            tasksInfo.isDownload = false
            tasksInfo.downloadedData = nil
            if let loc = saveToDirectory(downloadTask, location: location) {
                saveJsonSongsInDB(filename: loc, category: languages!.filter{$0.name == tasksInfo.name!}.first!)
            }
        }
    }
    downloadFileIndex -= 1
    if downloadFileIndex >= 0 {
        startDownloading(self.downloadTaskList[self.downloadFileIndex])
    } else {
        //Data fault
        if Constants.coreDataStack.updateAllCategoryData(newData: updatedCategoriesData) { //Data fault
            DispatchQueue.main.async {
                self.popOverView.removeFromSuperview()
                self.tableView.reloadData()
            }
        } else {
            DispatchQueue.main.async {
                self.popOverView.removeFromSuperview()
                self.showAlert(title: "Error", msg: "Error updating songs database")
            }
        }
    }
}

viewDidLoad的第一张照片很好

  

[   (实体:类别; id:0x2817ec400    ;   数据:{       coverPhotoBit64 =“ / 9j / 4RI + RXhpZgAATU0AKgAAAAgADAEAAAMAAAABAggAAAEBAAMAAAABAOwAAAECAAMAAAADAAAAngEGAAMAAAABAAIAAAESAAMAAAABAAEAAAEVAAMAAAABAAMAAAAEaAAUAAAABAAAApAEbAAUAUABAABAAAArAEoAAMAAAABAAIAAA       id = 6;       imgUrl =“ http://81.217.174.200/img/category/1540910884.jpg”;       jsonUrl =“ http://81.217.174.200/storage/french.json”;       名称=法文;       订单= 2;       歌曲=(       );       版本= 3; })] 1 [(实体:类别; ID:0x2817ec400    ;   数据:{       coverPhotoBit64 =“ / 9j / 4RI + RXhpZgAATU0AKgAAAAgADAEAAAMAAAABAggAAAEBAAMAAAABAOwAAAECAAMAAAADAAAAngEGAAMAAAABAAIAAAESAAMAAAABAAEAAAEVAAMAAAABAAMAAAAEaAAUAAAABAAAApAEbAAUAUABAABAAAArAEoAAMAAAABAAIAAA       id = 6;       imgUrl =“ http://81.217.174.200/img/category/1540910884.jpg”;       jsonUrl =“ http://81.217.174.200/storage/french.json”;       名称=法文;       订单= 2;       歌曲=(       );       版本= 3; })]

从扩展名中打印第二张,甚至当我从viewDidLoad

的其他位置打印时
> [<CategoryManagedObject: 0x2834849b0> (entity: Categories; id:
> >0x2817ec400 <x-coredata:///Categories/tC7259021-7A75-4C8F-A7D7->D7419A857F4A11> ;
> data: <fault>)]

我在这里有内存问题吗?也许是图像的bit64太大了?我需要知道发生了什么,下载完成后,我需要这些数据来更新类别数据库实体。

0 个答案:

没有答案