iOS 12错误:似乎来自与该上下文不同的NSManagedObjectModel [英] iOS 12 Errors: appears to be from a different NSManagedObjectModel than this context's

查看:50
本文介绍了iOS 12错误:似乎来自与该上下文不同的NSManagedObjectModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 12模拟器上启动我的应用程序时,我开始出现以下错误。有人遇到过这样的问题吗?

I started to get the following error when launching my application on iOS 12 simulator. Did anybody face issue like this?

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中定义了全局常量:

I have global constant defined in AppDelegate:

let viewContext = AppDelegate.viewContext

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

And use it with NSFetchedResultsController for UITableView update, for example:

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()
            }
        }
    }
}

同时看起来该应用程序可以运行,但是还没有机会正确测试所有内容。

In the same time it looks like that app works, but had no chance to test everything properly yet.

我使用CoreData,Seam3框架。

I use CoreData, Seam3 framework.

我在 github ,但看不到解决方案。

I found the only one mention of this error on github, but do not see solution.

推荐答案

我在iOS上遇到此错误12也。这就是我最终在项目中修复它的方式。这是在目标C中,而不是Swift中,但希望它将使您朝正确的方向前进。

I had been getting this error with iOS 12 also. This is how I finally fixed it in my project. This is in Objective C, not Swift, but hopefully it will get you going in the right direction.

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

The code that produced this error looked like this

// 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

The issue has something to do with the entity. So I updated my code to reflect the example on this page: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreData/FetchingObjects.html

这是我的代码现在看

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

没有更多似乎是来自与上下文不同的NSManagedObjectModel错误。

no more "appears to be from a different NSManagedObjectModel than this context" error.

这篇关于iOS 12错误:似乎来自与该上下文不同的NSManagedObjectModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆