当前位置 : 主页 > 手机开发 > 其它 >

swift – iOS 12错误:似乎来自与此上下文不同的NSManagedObjectModel

来源:互联网 收集:自由互联 发布时间:2021-06-11
在iOS 12模拟器上启动我的应用程序时,我开始出现以下错误.有没有人面临这样的问题? 2018-08-11 21:17:44.440144+0300 CafeManager[4633:128874] [error] error: The fetch request's entity 0x600001f6e940 'TablesTable
在iOS 12模拟器上启动我的应用程序时,我开始出现以下错误.有没有人面临这样的问题?

2018-08-11 21:17:44.440144+0300 CafeManager[4633:128874] [error] error: The fetch request's entity 0x600001f6e940 'TablesTable' appears to be from a different NSManagedObjectModel than this context's

我在AppDelegate中定义了全局常量:

let viewContext = AppDelegate.viewContext

并将它与NSFetchedResultsController一起用于UITableView更新,例如:

import UIKit
import CoreData

class HistoryTablesTableViewController: FetchedResultsTableViewController {
    //MARK: variables
    private var fetchedResultsController: NSFetchedResultsController<TablesTable>?
    private var currentTable: TablesTable?
    private var tableNameTextField: UITextField!

    //MARK: system functions for view
    override func viewDidLoad() {
        super.viewDidLoad()
        sideMenu()
        addSyncObserver()
    }
    override func viewWillAppear(_ animated: Bool) {
        updateGUI()
    }

    // MARK: IBOutlets
    @IBOutlet weak var menuButton: UIBarButtonItem!

    // MARK: side menu
    private func sideMenu() {
        if revealViewController() != nil {
            menuButton.target = revealViewController()
            menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
            revealViewController().rearViewRevealWidth = 260

            view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }
    }

    //MARK: functions for table update
    private func updateGUI () {
        let request : NSFetchRequest<TablesTable> = TablesTable.fetchRequest()
        request.sortDescriptors = [NSSortDescriptor(key: "tableName", ascending: true, selector: #selector(NSString.localizedStandardCompare(_:)))]
        fetchedResultsController = NSFetchedResultsController<TablesTable>(fetchRequest: request, managedObjectContext: viewContext, sectionNameKeyPath: nil, cacheName: nil)
        try? fetchedResultsController?.performFetch()
        tableView.reloadData()
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! HistoryTablesTableViewCell
        if let tablesTable = fetchedResultsController?.object(at: indexPath) {
            cell.tableNameLabel.text = tablesTable.tableName
            cell.cellDelegate = self
            cell.table = tablesTable
        }
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath as IndexPath)
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)
        currentTable = fetchedResultsController?.object(at: indexPath)
        performSegue(withIdentifier: "showTableSessions", sender: cell)
    }

    //MARK: prepare for segue
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showTableSessions" {
            if let tableSessionsTVC = segue.destination as? TableSessionsTableViewController {
                tableSessionsTVC.title = self.currentTable!.tableName!
                tableSessionsTVC.currentTable = self.currentTable!
            }
        }
    }
}

// MARK: Delegates
extension HistoryTablesTableViewController: HistoryTablesTableViewCellDelegate {
    func didPressTablesCellButton(table: TablesTable) {
        currentTable = table
    }
}

// Common extension for fetchedResultsController
extension HistoryTablesTableViewController {
    override func numberOfSections(in tableView: UITableView) -> Int {
        return fetchedResultsController?.sections?.count ?? 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let sections = fetchedResultsController?.sections, sections.count > 0 {
            return sections[section].numberOfObjects
        }
        else {
            return 0
        }
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if let sections = fetchedResultsController?.sections, sections.count > 0 {
            return sections[section].name
        }
        else {
            return nil
        }
    }

    override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return fetchedResultsController?.sectionIndexTitles
    }

    override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
        return fetchedResultsController?.section(forSectionIndexTitle: title, at: index) ?? 0
    }
}

// Observer to check that sync was performed to update GUI
extension HistoryTablesTableViewController {
    private func addSyncObserver () {
        NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: appDelegate.syncDidFinishNotification), object: nil, queue: nil) {
            [weak self] notification in
            DispatchQueue.main.async {
                self?.updateGUI()
            }
        }
    }
}

同时它看起来像应用程序工作,但没有机会正确测试一切.

我使用CoreData,Seam3框架.

我发现在github上只提到了这个错误,但没有看到解决方案.

iOS 12也遇到了这个错误.这就是我最终在我的项目中修复它的方法.这是在Objective C中,而不是Swift,但希望它会让你朝着正确的方向前进.

产生此错误的代码如下所示

// in the init code for my object
NSString *pathm = [[NSBundle mainBundle] pathForResource:@"mycoredb" ofType:@"momd"];
NSURL *momURL = [NSURL fileURLWithPath:pathm];
self.model = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];


// in another method in the object
NSFetchRequest *request = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [[self.model entitiesByName] objectForKey:@"Model_name"];
[request setEntity:entity];

该问题与实体有关.所以我更新了我的代码以反映此页面上的示例:https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreData/FetchingObjects.html

这是我的代码现在的样子

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Model_name"];

不再“看起来来自与此上下文不同的NSManagedObjectModel”错误.

网友评论