2 个实体: 用户和建筑物。
用户有一个名为building的NSSet类型的属性
我想通过属性guid从NSSet检索Buildings实体。
在应用程序第一次运行时,添加数据成功。可以正确检索和显示数据。应用程序关闭后,随后通过 guid 获取现有建筑物实体的尝试失败。第一次getBuildingsByGuid
返回nil
,因为没有实体;当我调用函数getBuildingsByGuid并且建筑物集中有实体时,应用程序崩溃。使用谓词作品获取;实体之间的关系是:一个用户对许多建筑物。
无法通过调试捕获指令。
在User对象中,以下自定义方法应获取Buildings:
- (Buildings *)getBuildingsByGuid:(NSString *)guid
{
for (Buildings *buildings in self.buildings)
{
if ([buildings.guid isEqualToString:guid])
return buildings;
}
return nil;
}
编辑:我的实体用户标头包含:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Buildings, TableBuildings;
@interface User : NSManagedObject
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * first_name;
@property (nonatomic, retain) NSString * guid_contact;
@property (nonatomic, retain) NSString * guid_user;
@property (nonatomic, retain) NSString * guid_userRole;
@property (nonatomic, retain) NSString * last_name;
@property (nonatomic, retain) NSString * password;
@property (nonatomic, retain) NSString * role_code;
@property (nonatomic, retain) NSString * role_name;
@property (nonatomic, retain) NSString * token;
@property (nonatomic, retain) NSString * type_code;
@property (nonatomic, retain) NSString * type_name;
@property (nonatomic, retain) NSSet *buildings;
@property (nonatomic, retain) NSSet *tableBuildings;
@end
@interface User (CoreDataGeneratedAccessors)
// Buildings
- (void)addBuildingsObject:(Buildings *)value;
- (void)removeBuildingsObject:(Buildings *)value;
- (void)addBuildings:(NSSet *)values;
- (void)removeBuildings:(NSSet *)values;
- (BOOL)containsBuildingsObject:(Buildings*)value;
- (Buildings *)getBuildingsByGuid:(NSString *)guid;
// TableBuildings
- (void)addTableBuildingsObject:(TableBuildings *)value;
- (void)removeTableBuildingsObject:(TableBuildings *)value;
- (void)addTableBuildings:(NSSet *)values;
- (void)removeTableBuildings:(NSSet *)values;
- (BOOL)containsTableBuildingsObject:(TableBuildings*)value;
- (TableBuildings *)getTableBuildingsByGuid:(NSString *)guid;
@end
实现文件:
#import "User.h"
#import "Buildings.h"
#import "TableBuildings.h"
@implementation User
@dynamic email;
@dynamic first_name;
@dynamic guid_contact;
@dynamic guid_user;
@dynamic guid_userRole;
@dynamic last_name;
@dynamic password;
@dynamic role_code;
@dynamic role_name;
@dynamic token;
@dynamic type_code;
@dynamic type_name;
@dynamic buildings;
@dynamic tableBuildings;
@end
@implementation User (CoreDataGeneratedAccessors)
#pragma mark - Buildings management methods
- (void)addBuildingsObject:(Buildings *)value
{
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"buildings" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"buildings"] addObject:value];
[self didChangeValueForKey:@"buildings" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[changedObjects release];
}
- (void)removeBuildingsObject:(Buildings *)value
{
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"buildings" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"buildings"] removeObject:value];
[self didChangeValueForKey:@"buildings" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
[changedObjects release];
}
- (void)addBuildings:(NSSet *)values
{
[self willChangeValueForKey:@"buildings" withSetMutation:NSKeyValueUnionSetMutation usingObjects:values];
[[self primitiveValueForKey:@"buildings"] unionSet:values];
[self didChangeValueForKey:@"buildings" withSetMutation:NSKeyValueUnionSetMutation usingObjects:values];
}
- (void)removeBuildings:(NSSet *)values
{
[self willChangeValueForKey:@"buildings" withSetMutation:NSKeyValueUnionSetMutation usingObjects:values];
[[self primitiveValueForKey:@"buildings"] unionSet:values];
[self didChangeValueForKey:@"buildings" withSetMutation:NSKeyValueUnionSetMutation usingObjects:values];
}
- (BOOL) containsBuildingsObject:(Buildings*)value
{
for (Buildings *buildings in self.buildings)
{
if( [buildings.guid isEqualToString:value.guid])
return YES;
}
return NO;
}
- (Buildings *)getBuildingsByGuid:(NSString *)guid
{
for (Buildings *buildings in self.buildings)
{
if ([buildings.guid isEqualToString:guid])
return buildings;
}
return nil;
}
#pragma mark - TableBuildings management methods
- (void)addTableBuildingsObject:(TableBuildings *)value
{
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"tableBuildings" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"tableBuildings"] addObject:value];
[self didChangeValueForKey:@"tableBuildings" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[changedObjects release];
}
- (void)removeTableBuildingsObject:(TableBuildings *)value
{
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"tableBuildings" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"tableBuildings"] removeObject:value];
[self didChangeValueForKey:@"tableBuildings" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
[changedObjects release];
}
- (void)addTableBuildings:(NSSet *)values
{
[self willChangeValueForKey:@"tableBuildings" withSetMutation:NSKeyValueUnionSetMutation usingObjects:values];
[[self primitiveValueForKey:@"tableBuildings"] unionSet:values];
[self didChangeValueForKey:@"tableBuildings" withSetMutation:NSKeyValueUnionSetMutation usingObjects:values];
}
- (void)removeTableBuildings:(NSSet *)values
{
[self willChangeValueForKey:@"tableBuildings" withSetMutation:NSKeyValueUnionSetMutation usingObjects:values];
[[self primitiveValueForKey:@"tableBuildings"] unionSet:values];
[self didChangeValueForKey:@"tableBuildings" withSetMutation:NSKeyValueUnionSetMutation usingObjects:values];
}
-(BOOL) containsTableBuildingsObject:(TableBuildings*)value
{
for (TableBuildings *tableBuildings in self.tableBuildings)
{
if( [tableBuildings.guid_building isEqualToString:value.guid_building])
return YES;
}
return NO;
}
- (TableBuildings *)getTableBuildingsByGuid:(NSString *)guid
{
for (TableBuildings *tableBuildings in self.tableBuildings)
{
if ([tableBuildings.guid_building isEqualToString:guid])
return tableBuildings;
}
return nil;
}
@结尾
当应用程序启动时,将通过电子邮件和密码获取用户:
user = [[DataController instance] fetchEntityOfType:object_User withPredicate:@"email = %@ AND password = %@", username, passwordMD5];
稍后,当我想显示我的建筑物列表时,我调用 getBuildingsByGuid 方法,如下所示:
// buildings is parsed from json and have NSDictionary type
for (NSDictionary *building in buildings)
{
NSString *guid_building = [building safeObjectForKey:@"guid"];
// get building and check if is associated to the current user
BuildingPerUser *buildingPerUser = [[DataController instance] fetchEntityOfType:object_BuildingPerUser withPredicate:@"guid_user = %@ AND guid_building = %@", guid_user, guid_building];
//create new association if not exists
if (!buildingPerUser)
{
buildingPerUser = [[[DataController instance] newEnityOfType:object_BuildingPerUser] autorelease];
buildingPerUser.guid_user = guid_user;
buildingPerUser.guid_building = guid_building;
}
Buildings *buildingRow = [user getBuildingsByGuid:guid_building];
// create new building if not exists
if (!buildingRow)
{
buildingRow = [[[DataController instance] newEnityOfType:object_Buildings] autorelease];
buildingRow.guid = guid_building;
[user addBuildingsObject:buildingRow];
}
// update building
buildingRow.name = [building safeObjectForKey:@"name"];
buildingRow.isNew = [NSNumber numberWithBool:NO];
[delComplementBuildings addObject:guid_building];
}
如果我使用:
Buildings *buildingRow = [[DataController instance] fetchEntityOfType:object_Buildings withPredicate:@"guid = %@", guid_building];
...我的应用程序工作。和:
Buildings *buildingRow = [user getBuildingsByGuid:guid_building];
..应用程序崩溃。
我更喜欢使用对象而不是谓词获取......
另一个编辑:我发现了异常:关于建筑物的 nsset 实体的关系错误 下面是建筑物的列表:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class TableBuildings, User;
@interface Buildings : NSManagedObject
@property (nonatomic, retain) NSString * guid;
@property (nonatomic, retain) NSNumber * isNew;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *tableBuildings;
@property (nonatomic, retain) User *user;
@end
@interface Buildings (CoreDataGeneratedAccessors)
- (void)addTableBuildingsObject:(TableBuildings *)value;
- (void)removeTableBuildingsObject:(TableBuildings *)value;
- (void)addTableBuildings:(NSSet *)values;
- (void)removeTableBuildings:(NSSet *)values;
@end
#import "Buildings.h"
#import "TableBuildings.h"
#import "User.h"
@implementation Buildings
@dynamic guid;
@dynamic isNew;
@dynamic name;
@dynamic tableBuildings;
@dynamic user;
@end
建筑物访问器尚未实施。
问题已解决。初始化用户时出错。解决方案:为用户使用属性。