开发者

If I have a retained NSManagedObject, how to check if one of it's OPTIONAL relationships has values or not?

I have an NSManagedObject called account and this is returned and retained from a fetch. This NSManagedObject has an optional relationship called reports which is a To-Many relationship the Reports entity. An account can have many reports.

An account entity, when created though, will not have any reports. When I first pass along this newly created object account, how do I properly check 开发者_如何学运维that it has NO reports?

I've tried both:

if ([self.account valueForKey:@"reports"] == nil)

if ([self.account valueForKey:@"reports"] == NULL)

NSLog when checked for [self.account valueForKey:@"reports"] returns a Relationship Fault in console. I've read this is normal since it has no values or any Report entities associated to it yet.

How do I programmatically check this though ?


Since this is a to-many relationship, it is represented by an NSMutableSet object (technically a CoreData specific subclass, but that isn't important). This object will always be there, even if there are no objects in it, and what you see in the console is its description. You need to call count to see how many objects are in the reports relationship.

if([[self.account valueForKey:@"reports"] count] == 0) [self noReports];

Checking against nil, like you did in the question, works for to-one relationships.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜