Mac OSX - 不存储核心数据 [英] Mac OSX - Core data isn't stored

查看:265
本文介绍了Mac OSX - 不存储核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用两个表视图绑定到每个 NSArrayController 和数组控制器设置为使用核心数据实体。当生成数据时,将创建一个 NSObject 并将值存储在其中 obj.setValue(_:forKey:)。之后,该对象简单地添加到数组控制器 ac.addObject()



是否足够让Core Data处理数据的持久存储?



在任何情况下,如果我尝试通过调用saveAction()来保存数据, MOC没有更改( moc.hasChanges = false ),因此它甚至不会使用此方法开始保存数据。


$ b $



在Interface Builder中设置数组控制器的方法如下: :




  • 模式:实体名称

  • 实体名称: / li>
  • 准备检查内容



它们也正确地绑定到托管对象上下文。 p>

我的应用程式的简化相关程式码:

  / *清除现有资料。 * / 
let range:NSRange = NSMakeRange(0,arrayController.arrangedObjects.count);
let indexSet:NSIndexSet = NSIndexSet(indexesInRange:range);
arrayController.removeObjectsAtArrangedObjectIndexes(indexSet);

let array = generateData();

/ *生成数据。 * /
for i in 0 ..< array.count
{
let data = array [i];

/ *创建新的数据对象。 * /
var obj:NSObject = arrayController.newObject()as! NSObject;
obj.setValue(data.name,forKey:name);
obj.setValue(data.type,forKey:type);
obj.setValue(data.category,forKey:category);

/ *将它添加到数组控制器的contentArray。 * /
arrayController.addObject(obj);
}

UPDATE:



看起来我的应用程序在启动时实例化了四个MOC。我怀疑,我如何添加他们在故事板中的两个阵列控制器的方式是错误的。我添加了一个NSObject到两个表视图控制器(它们还包含它们的数组控制器),并将其基类设置为我的 CoreDataDelegate (这是我的类的核心数据代码通常在 AppDelegate )。我怀疑这是创建 CoreDataDelegate 的多个实例。问题是:我应该如何做到这一点,以便数组控制器可以到达我的 CoreDataDelegate 类?

我让我的核心数据委托类一个Singleton解决了我的问题...

  class CoreDataDelegate:NSObject 
{
static let instance = CoreDataDelegate();
...
}

然后我在我的AppDelegate(也是单例)...

  @NSApplicationMain 
class AppDelegate:NSObject,NSApplicationDelegate
{
static let instance = AppDelegate();
let coreData:CoreDataDelegate;

override init()
{
coreData = CoreDataDelegate.instance;
super.init();
}

...
}

然后在故事板中,我向我的两个表视图控制器添加了一个NSObject,并将基类设置为AppDelegate。然后通过AppDelegate / coreData.moc将ArrayControllers绑定到moc。



现在只创建一个CoreDataDelegate实例(因此只有一个moc),我可以高兴地报告现在储蓄工作!


In my app I use two table views that are bound to each their NSArrayController and the array controllers are set to use Core Data entities. When data is generated, an NSObject is created and values are stored in it with obj.setValue(_:forKey:). After this the object is simply added to the array controller with ac.addObject().

Shouldn't this suffice to have Core Data taking care of persistent storage of the data?

In any case, if I try to save the data by calling saveAction() it tells me that the MOC has no changes (moc.hasChanges = false) so it doesn't even begin to save the data with this method.

What else do I need to take care of to make Core Data store the data properly and acknowledge changes?

The array controllers are set in Interface Builder as follows:

  • Mode: Entity Name
  • Entity Name: 'name of entity in data model'
  • Prepares Content is checked

They are also correctly bound to the managed object context.

Simplified, relevant code from my app:

    /* Clear existing data. */
    let range:NSRange = NSMakeRange(0, arrayController.arrangedObjects.count);
    let indexSet:NSIndexSet = NSIndexSet(indexesInRange: range);
    arrayController.removeObjectsAtArrangedObjectIndexes(indexSet);

    let array = generateData();

    /* Generate data. */
    for i in 0 ..< array.count
    {
        let data = array[i];

        /* Create new data object. */
        var obj:NSObject = arrayController.newObject() as! NSObject;
        obj.setValue(data.name, forKey: "name");
        obj.setValue(data.type, forKey: "type");
        obj.setValue(data.category, forKey: "category");

        /* Add it to the array controller's contentArray. */
        arrayController.addObject(obj);
    }

UPDATE:

It looks like my app is instantiating four MOCs when it launches. I suspect that the way how I add them in the Storyboard for the two array controllers is wrong. I added an NSObject to the two table view controllers (which also contain their array controllers) and set their base classes to be my CoreDataDelegate (which is my class for the core data code that is normally in AppDelegate). I suspect this is where the multiple instances of CoreDataDelegate are created. The question is: How should I do this right so that the array controllers can reach my CoreDataDelegate class?

解决方案

I made my core data delegate class a Singleton which solved the problem for me...

class CoreDataDelegate : NSObject
{
    static let instance = CoreDataDelegate();
    ...
}

Then I have a reference to it in my AppDelegate (which is also a singleton) ...

@NSApplicationMain
class AppDelegate : NSObject, NSApplicationDelegate
{
    static let instance = AppDelegate();
    let coreData:CoreDataDelegate;

    override init()
    {
        coreData = CoreDataDelegate.instance;
        super.init();
    }

    ...
}

Then in the storyboard I added an NSObject to my two table view controllers and set the base class to AppDelegate. And then bound the ArrayControllers to the moc via AppDelegate/coreData.moc.

Now only one instance of CoreDataDelegate is created (and therefore only one moc) and I can happily report that saving works now!

这篇关于Mac OSX - 不存储核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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