获取属性作为NSManagedObject子类的属性 [英] Fetched properties as properties of an NSManagedObject subclass

查看:84
本文介绍了获取属性作为NSManagedObject子类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Team的NSManagedObject(NSMO)的子类。团队与另一个名为Contract的NSMO子类有一对多的关系。合同与玩家(另一个NSMO)有1对1。我想简化我的代码,使用Team,并能够参考它的玩家。

I have a subclass of NSManagedObject (NSMO) called Team. Team has a one to many relationship with another NSMO subclass called Contract. Contract has a 1-to-1 with Player (another NSMO). I would like to simplify my code that uses Team and be able to just refer to its "players". Players will just be an array of players that have a contract with a given team.

我已经尝试在XCode的数据建模工具中创建一个名为players的获取属性,但没有什么成功。我尝试了许多不同的方法,但是最有意义的是把它命名为players,目标为Player,最后有我的谓词contract.team.name == SELF。

I've tried creating a fetched property called players in XCode's data modeling tool with little success. I've tried many different approaches, but the one that makes the most sense is to name it "players", have the destination as Player and finally have as my predicate "contract.team.name == SELF".

在我的Team类中,我有一个属于NSArray的名为players的玩家(在实现中是@dynamic玩家)。当我NSLog(@%@,self.players)它退出...

In my Team class I have a property of NSArray called players (with is @dynamic players in the implementation). When I NSLog(@"%@", self.players) it logs out...

Relationship fault for (<NSFetchedPropertyDescription: 0x6d19cd0>), name players, isOptional 1, isTransient 1, entity Team, renamingIdentifier players, validation predicates (
), warnings (
), versionHashModifier (null), fetchRequest <NSFetchRequest: 0x6d1a080> (entity: Player; predicate: (contract.team.name == SELF); sortDescriptors: ((null)); type: NSManagedObjectResultType; ) on 0x6d38550

...这对我来说没有意义。我觉得我做错了很多事情,我甚至不知道从哪里开始处理它。

...which makes no sense to me. I feel like I'm doing so many things wrong I don't even know where to begin tackling it.

所以我想我的问题是
1)这个获取的属性应该在我的NSMO子类中采用什么类型。 (我做了NSArray,但显然是一个NSFetchedPropertyDescription)
2)我可以使用什么代码来获取数组或设置?

So I guess my questions are 1) What type should this fetched property assume in my NSMO subclass. (I made it NSArray but apparently it's a NSFetchedPropertyDescription) 2) What code can I use to get an array or set out?

谢谢! Rob

推荐答案

获取关系的正常回报。你有一个故障,即一个对象的幽灵。在您尝试直接访问其属性之前,不会加载实际的托管对象。

Your getting the normal return for a fetched relationship. What you have is a fault i.e. the ghost of an object. The actual managed objects are not loaded in until you try to access one their attributes directly.

核心数据使用关系中的错误,以防止必须将大量对象加载到内存中,只是为了找到一小块数据。假设您与另一端有1,000个受管对象有关系,并且您需要从这些受管对象之一获取一个整数值。没有故障,Core Data将不得不将1,000个对象加载到内存中,以查找和检索32位数据。有了错误,Core Data知道 其中 整数值是,可以得到一个对象及其一个值。

Core Data uses faults in relationships to prevent having to load a vast number of objects into memory just to find one small piece of data. Suppose you have a relationship with a 1,000 managed objects on the other side and you need one integer value from one of those managed objects. Without faulting, Core Data would have to load 1,000 objects into memory just to find and retrieve 32-bits of data. With faulting, Core Data knows where the integer value is and can go get that one object with its one value.

你可以像一个数组一样处理一个获取的属性。以这个简单的托管对象子类为例,其中 theFetchedProperty 是一个获取的属性,其谓词为TestEntity.order> 5

You can treat a fetched property just like an array. Take the case of this simple managed object subclass in which theFetchedProperty is a fetched property with a predicate of "TestEntity.order>5"

@interface TestEntityMO :  NSManagedObject  
{}
@property (nonatomic, retain) NSNumber * order;
@property(nonatomic, retain)  NSArray *theFetchedProperty;

设置如下:

TestEntityMO *testMO;
for (int i=0; i<10; i++) {
    testMO=[NSEntityDescription insertNewObjectForEntityForName:@"TestEntity" inManagedObjectContext:self.managedObjectContext];
    testMO.order=[NSNumber numberWithInt:i];
}

然后记录创建的最后一个TestEntityMO对象。 第一次记录该属性,它返回一个错误:

Then log the the last TestEntityMO object created. The first time you log the property, it returns a fault:

NSLog(@"testMO.theFetchedProperty == %@",testMO.theFetchedProperty);

输出

testMO.theFetchedProperty == Relationship fault for (<NSFetchedPropertyDescription: 0x3d19210>), //... rest removed for brevity

...但你可以像一个数组一样记录获取的属性的数量:

... but you can log the count of the fetched property just like an array:

NSLog(@"[testMO.theFetchedProperty count] == %d",[testMO.theFetchedProperty count] );

...输出:

[testMO.theFetchedProperty count] == 4

对象在索引就像一个数组:

You can get the object at an index just like an array:

NSLog(@"[testMO.theFetchedProperty objectAtIndex:0] == %@",[testMO.theFetchedProperty objectAtIndex:0]);

...输出TestEntityMO对象的描述(可能或可能不是故障。

... outputs the description of a TestEntityMO object (which may or may not be fault. In this case not):

[testMO.theFetchedProperty objectAtIndex:0] == <TestEntityMO: 0x3d20a70> (entity: TestEntity; id: 0x3d20ab0 <x-coredata:///TestEntity/t3A79EE49-39F4-4FCA-8E25-0C28B8E0E01A11> ; data: {
    order = 9;
    theFetchedProperty =     (
        0x3d20ab0 <x-coredata:///TestEntity/t3A79EE49-39F4-4FCA-8E25-0C28B8E0E01A11>,
        0x3d20a00 <x-coredata:///TestEntity/t3A79EE49-39F4-4FCA-8E25-0C28B8E0E01A10>,
        0x3d20880 <x-coredata:///TestEntity/t3A79EE49-39F4-4FCA-8E25-0C28B8E0E01A8>,
        0x3d20970 <x-coredata:///TestEntity/t3A79EE49-39F4-4FCA-8E25-0C28B8E0E01A9>
    );
})

记录随机对象的属性:

NSLog(@"[[testMO.theFetchedProperty objectAtIndex:0] order] == %@",[[testMO.theFetchedProperty objectAtIndex:1] order]);

...输出:

[[testMO.theFetchedProperty objectAtIndex:1] order] == 8

,如果您在上述代码触发抓取后第二次抓取抓取的媒体资源,则会获得实际对象:

However, if you log the fetched property a second time after you trigger the fetch by the code above you get a actual objects:

NSLog(@"testMO.theFetchedProperty == %@",testMO.theFetchedProperty);

...输出:

testMO.theFetchedProperty == Relationship objects for (
    <TestEntityMO: 0x3d20a70> ... ; 
data: {
    order = 9;
    theFetchedProperty =     (
        0x3d20ab0 <x-coredata:///TestEntity/t3A79EE49-39F4-4FCA-8E25-0C28B8E0E01A11>,
        0x3d20a00 <x-coredata:///TestEntity/t3A79EE49-39F4-4FCA-8E25-0C28B8E0E01A10>,
        0x3d20880 <x-coredata:///TestEntity/t3A79EE49-39F4-4FCA-8E25-0C28B8E0E01A8>,
        0x3d20970 <x-coredata:///TestEntity/t3A79EE49-39F4-4FCA-8E25-0C28B8E0E01A9>
    );
}),...

总之,您可以:


  1. 像代码中的NSArrays一样处理提取的关系。

  2. 与所有管理对象一样,如果直接记录对象,您将经常看到故障。

这篇关于获取属性作为NSManagedObject子类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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