CoreData问题

时间:2019-04-07 07:32:43

标签: ios swift core-data

现在我要做我的RSSReader应用程序,需要在其上添加CoreData。 我使用pod FeedKit,它具有RSSFeed类,该类从chanel接收数据。我需要将该数据保存到CoreData,然后显示在我的应用程序上。我有一个DataManager,它具有CoreData中的saveChanel方法。

我在DataManager上获得的数据。 DataManager具有方法loadChanel,该方法具有来自CoreData的方法saveChanel(PersistanceManager.shared.saveChanell(feed:RSSFeed))。 我的coreDataModel有2个实体“ Item”和“ Chanel”。

我知道我在PersistanceManager上的代码中有问题(下面的代码)。有人可以帮忙吗?

我的DataManager是:

class DataManager {

    static let sharedInstance = DataManager()

//  var dataManagerDelegate: DataManagerDelegate?
    var myLinkString: String!

    private init() {}

    // MARK: - Public

    func loadChanel(completion: @escaping ([Chanel]?, Error?, Bool) -> Void, channelAddres: String) {

        guard let myLink = myLinkString, let linkUrl = URL(string: myLink) else { return }
        let parser = FeedParser(URL: linkUrl)

        let result = parser.parse()

        guard let feed = result.rssFeed, result.isSuccess else { return }
        PersistanceManager.shared.saveChanell(feed: RSSFeed)
    }

    func loadChannels(completion: @escaping ([Chanel]?, Error?, Bool) -> Void) {

        func performCompletion(channels: [Chanel]?, error: Error?, finished: Bool) {
            DispatchQueue.main.async {
                completion(channels, error, finished)
            }
        }

        /*
        * Fetch local channels
        */
        let cachedChannels = PersistanceManager.shared.fetchRssChannels()
        performCompletion(channels: cachedChannels, error: nil, finished: false)

        /*
        * Load channels from server
        */
            myLinkString = "https://..."

            guard let linkUrl = URL(string: myLinkString) else { return }
            let parser = FeedParser(URL: linkUrl)

            let result = parser.parse()

            guard let feed = result.rssFeed, result.isSuccess else { return }

        /*
        * Store data to database
        */
            PersistanceManager.shared.saveChanell(channel: Chanel, feed: RSSFeed)

        /*
        * Get actual posts from data base and return
        */
        let updatedChannels = PersistanceManager.shared.fetchRssChannels()
        performCompletion(channels: updatedChannels, error: nil, finished: true)
    }

    func saveContext() {
        PersistanceManager.shared.saveContext()
    }

}

在DataManager上,以下几行出现3个错误:

    PersistanceManager.shared.saveChanell(feed: RSSFeed)
    PersistanceManager.shared.saveChanell(channel: Chanel, feed: RSSFeed)

错误是: “无法将类型'RSSFeed.Type'的值转换为预期的参数类型'RSSFeed'”); “无法将类型“ Chanel.Type”的值转换为预期的参数类型“ Chanel””

和PersistanceManager:

import Foundation
import CoreData
import FeedKit

class PersistanceManager {

    // wrapper for core data

    static var shared = PersistanceManager()

    private init() {}

    func saveChanell(feed: RSSFeed) {

        let channel = createNewChanel(with: feed.title)

        channel?.rssDescription = feed.description
        channel?.pubdate = feed.pubDate! as NSDate

        channel?.link = feed.link
        channel?.language = feed.language
        channel?.creator = feed.dublinCore?.dcCreator

        guard let feedItems = feed.items else {
            return
        }

        guard let _ = channel?.item else {
            channel?.item = NSSet()
        }

        guard let channelItems = channel?.item else {
            return
        }

        for feedItem in feedItems {
        }

        // 5
        do {
            try context.save()
            print("Channel saved")
        } catch {}
    }

    func saveChanell(channel: Chanel, feed: RSSFeed) {

        channel.title = feed.title
        channel.rssDescription = feed.description
        channel.pubdate = feed.pubDate! as NSDate
        //channel.pubDate = NSDate(timeIntervalSince1970: feed.pubDate?.timeIntervalSince1970)

        channel.link = feed.link
        channel.language = feed.language
        //channel.isLastUsed
        channel.creator = feed.dublinCore?.dcCreator

        guard let items = feed.items else {
            return
        }

        // 3
        for item in items {
            guard let mediaLink = item.media?.mediaThumbnails?.first?.attributes?.url else {
                continue
            }
            let rssItem = createRssItem(with: mediaLink, in: context)

            rssItem?.title = item.title
            //      rssItem?.pubdate = (item.pubDate as! NSDate)
            rssItem?.link = item.link
            rssItem?.itemDescription = item.description
            rssItem?.category = item.categories?.first?.value

            channel.item?.adding(rssItem as Any)
        }

        // 5
        do {
            try context.save()
            print("Channel saved")
        } catch {}
    }

    // 4
    private func createRssItem(with mediaLink: String, in context: NSManagedObjectContext) -> Item? {

        let newRssItem = NSEntityDescription.insertNewObject(forEntityName: "Item", into: context) as? Item
        newRssItem?.mediaLink = mediaLink
        print("RSS Item created")

        return newRssItem
    }

    func fetchRssChannels() -> [Chanel]? {

        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Chanel")

        do {
            let channels = try self.context.fetch(fetchRequest) as? [Chanel]
            return channels
        } catch let error {
            print(error)
        }
        print("RSS channels fetched")

        return nil

    }

    // 2 - а тот ли тут канал
    func createNewChanel(with chanel: Chanel) -> Chanel? {

        if let findRssChannelCD = findRssChannel(title: chanel.title) {
            print("findRssChannelCD")
            return findRssChannelCD
        }

        let newRssChannelCD = NSEntityDescription.insertNewObject(forEntityName: "Chanel", into: context) as? Chanel
        newRssChannelCD?.title = chanel.title
        newRssChannelCD?.item = NSSet()

        do {
            try context.save()
            print("newChanelSaved")
        } catch {}

        return newRssChannelCD

    }

    // 1
    private func findRssChannel(title: String?) -> Chanel? {
        guard let title = title else {
            return nil
        }

        let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Chanel")
        request.predicate = NSPredicate(format: "title = %@", title)

        do {
            let users = try context.fetch(request) as? [Chanel]
            return users?.first

        } catch {}
        return nil
    }

    // MARK: - Core Data stack
    var context: NSManagedObjectContext {
        return persistentContainer.viewContext
    }

    lazy var persistentContainer: NSPersistentContainer = {
        /*
        The persistent container for the application. This implementation
        creates and returns a container, having loaded the store for the
        application to it. This property is optional since there are legitimate
        error conditions that could cause the creation of the store to fail.
        */
        let container = NSPersistentContainer(name: "NewsForIphone")
        container.loadPersistentStores(completionHandler: { (_, error) in
            if let error = error as NSError? {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate.
                //You should not use this function in a shipping application, although it may be useful during development.

                /*
                Typical reasons for an error here include:
                * The parent directory does not exist, cannot be created, or disallows writing.
                * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                * The device is out of space.
                * The store could not be migrated to the current model version.
                Check the error message to determine what the actual problem was.
                */
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()

    // MARK: - Core Data Saving support

    public func saveContext () {
        let context = persistentContainer.viewContext
        if context.hasChanges {
            do {
                try context.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate.
                //You should not use this function in a shipping application, although it may be useful during development.
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }
    }
}

我知道我在PersistanceManager上的代码中有问题(下面的代码),并且在DataManager中有错误。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

问题是由以下行引起的:

PersistanceManager.shared.saveChanell(feed: RSSFeed)

您要指定实际的类名而不是对象,您需要编写:

PersistanceManager.shared.saveChanell(feed: feed)