NSManagedObject 层次结构导入和导出
我正在努力使我的 NSMangedObjectClass 配置文件可导出/可导出。
我尝试这样
如果我在 NSArray 中写入关系,则导出可以正常工作,因为 NSSet 没有实现 writeToFile 。
- (void) exportProfile:(Profile *)profile toPath:(NSString *)path{
//Profile
NSMutableDictionary *profileDict = [[self.selectedProfile dictionaryWithValuesForKeys:[[[self.selectedProfile entity] attributesByName] allKeys]] mutableCopy];
NSMutableArray *views = [NSMutableArray array];
//Views
for (View *view in selectedProfile.views) {
NSMutableDictionary *viewDict = [[view dictionaryWithValuesForKeys:[[[view entity] attributesByName] allKeys]] mutableCopy];
NSMutableArray *controls = [NSMutableArray array];
//Much more for-loops
[viewDict setObject:controls forKey:@"controls"];
[views addObject:viewDict];
}
[profileDict setObject:views forKey:@"views"];
if([profileDict writeToFile:[path stringByStandardizingPath] atomically:YES])
NSLog(@"Saved");
else
NSLog(@"Not saved");
[profileDict release];
}
但是如果想在另一端导入
- (Profile*) importProfileFromPath:(NSString *)path{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
Profile *newProfile = [NSEntityDescription insertNewObjectForEntityForName:@"Profile" inManagedObjectContext:context];
NSMutableDictionary *profileDict = [NSMutableDictionary dictionaryWithContentsOfFile:[path stringByStandardizingPath]];
[newProfile setValuesForKeysWithDictionary:profileDict];
}
,我会得到一个异常,这不会让我感到困惑,因为 Profile 需要一个 NSSet 而不是 NSArray。[__NSCFArray intersectsSet:]:无法识别的选择器发送到实例 0x4e704c0 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,
原因:'-[__NSCFArray intersectsSet:]:无法识别的选择器发送到实例 0x4e704c0'
所以我有两个问题:
- 一方面,我无法将 NSSet 写入文件。
- 另一边是我的 Profile 类,需要一个 NSSet。
所以我尝试创建一个实现 writeToFile 的 NSSet 类别,
@implementation NSSet(Persistence)
- (BOOL)writeToFile:(NSString*)path atomically:(BOOL)flag{
NSMutableArray *temp = [NSMutableArray arrayWithCapacity:self.count];
for(id element in self)
[temp addObject:element];
return [temp writeToFile:path atomically:flag];
}
+ (id)setWithContentsOfFile:(NSString *)aPath{
return [NSSet setWithArray:[NSArray arrayWithContentsOfFile:aPath]];
}
@end
但我的函数没有被调用。
还有其他方法可以编写我的 NSSet 或告诉 setValuesForKeysWithDictionary 密钥“views”是 NSArray 吗?
或者导入/导出 ManagedObjects 的简单方法?
I'm on the run to make my NSMangedObjectClass profile im-/exportable.
I try it this way
Exporting works correctly if I write the Relationships in NSArrays because NSSet doesn't have writeToFile
implemented.
- (void) exportProfile:(Profile *)profile toPath:(NSString *)path{
//Profile
NSMutableDictionary *profileDict = [[self.selectedProfile dictionaryWithValuesForKeys:[[[self.selectedProfile entity] attributesByName] allKeys]] mutableCopy];
NSMutableArray *views = [NSMutableArray array];
//Views
for (View *view in selectedProfile.views) {
NSMutableDictionary *viewDict = [[view dictionaryWithValuesForKeys:[[[view entity] attributesByName] allKeys]] mutableCopy];
NSMutableArray *controls = [NSMutableArray array];
//Much more for-loops
[viewDict setObject:controls forKey:@"controls"];
[views addObject:viewDict];
}
[profileDict setObject:views forKey:@"views"];
if([profileDict writeToFile:[path stringByStandardizingPath] atomically:YES])
NSLog(@"Saved");
else
NSLog(@"Not saved");
[profileDict release];
}
But if want to import on the other side
- (Profile*) importProfileFromPath:(NSString *)path{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
Profile *newProfile = [NSEntityDescription insertNewObjectForEntityForName:@"Profile" inManagedObjectContext:context];
NSMutableDictionary *profileDict = [NSMutableDictionary dictionaryWithContentsOfFile:[path stringByStandardizingPath]];
[newProfile setValuesForKeysWithDictionary:profileDict];
}
I get an exception, this doesn't confuse me cause Profile expects a NSSet and no NSArray.[__NSCFArray intersectsSet:]: unrecognized selector sent to instance 0x4e704c0 *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFArray intersectsSet:]: unrecognized selector sent to instance 0x4e704c0'
So I've got two problems:
- On the one side, I can't write a NSSet to File.
- On the other side is my Profile class expecting an NSSet.
So I tried to create a Category of NSSet which implements writeToFile
@implementation NSSet(Persistence)
- (BOOL)writeToFile:(NSString*)path atomically:(BOOL)flag{
NSMutableArray *temp = [NSMutableArray arrayWithCapacity:self.count];
for(id element in self)
[temp addObject:element];
return [temp writeToFile:path atomically:flag];
}
+ (id)setWithContentsOfFile:(NSString *)aPath{
return [NSSet setWithArray:[NSArray arrayWithContentsOfFile:aPath]];
}
@end
But my functions aren't called.
Is there a other way to write my NSSet or tell setValuesForKeysWithDictionary
the Key "views" is a NSArray?
Or a easy Way to im-/export ManagedObjects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嵌套的 NSDictonary 出现问题,所以我告别了动态方式。
这是我帮助他人的完整解决方案
ViewController 调用 im/export 函数
NSManagedObject 函数 我有一个层次结构,所以我将它们放入每个 NSManagedObjects 中,
这不是最漂亮的解决方案,但它有效:)
我仍然需要弄清楚如何避免我的 n:m 关系中的重复
谢谢
Got problems with the nested NSDictonarys, so i said goodbye to the dynamic way.
Here is my complete Solution to help others
ViewController to call the im/export functions
NSManagedObject functions i got a hierarchy so i put them in each of my NSManagedObjects
not the most beautiful solution but it works :)
and i have still to figure out how to avoid duplicates in my n:m relationship
Thanks
您可以尝试覆盖 NSManagedObject 的 setValuesForKeysWithDictionary 的默认实现?
查看文档 你应该只需要实现 setValue:forKey: 在你的子类中?
您应该能够在抛出异常之前抓取其中的 NSSet 并自行处理它?
[免责声明 - 我从来没有这样做过!]
You could try overriding NSManagedObject's default implementation of setValuesForKeysWithDictionary ?
Looking at the documentation you should only have to implement setValue:forKey: in your subclasses?
You should be able grab the NSSet in there and deal with it yourself before the exception gets thrown?
[Disclaimer - I have never done this!]