我想用一个plist文件中的数据填充UITableView到核心数据
所以我很容易在更高版本中更新uitableview,然后我只需改变plist生活,核心数据就可以完成其余的工作。请告诉我是否有更好的方法...
无论如何,我在这里做的工作不正常,在第一次运行它填充核心数据但UITableView是空的,然后如果你完全关闭应用程序,也从多任务栏的东西,并启动它现在再次显示带有数据的uitableview
在我的app delegate.h中,我有:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if ( ![userDefaults valueForKey:@"version"] )
{
[self value1];
[self value2];
[self value3];
[self value4];
[self value5];
[self value6];
NSLog(@"running code");
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
// Adding version number to NSUserDefaults for first version:
[userDefaults setFloat:[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] floatValue] forKey:@"version"];
}
if ([[NSUserDefaults standardUserDefaults] floatForKey:@"version"] == [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] floatValue] )
{
/// Same Version so dont run the function
}
else
{
[Hovedmenu MR_truncateAll];
[BarneDaab MR_truncateAll];
[Graviditeten MR_truncateAll];
[MineSygedomme MR_truncateAll];
[Fortalt MR_truncateAll];
[Familien MR_truncateAll];
[self value2];
[self value3];
[self value4];
[self value5];
[self value6];
[self value1];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
NSLog(@"running code agian");
// Update version number to NSUserDefaults for other versions:
[userDefaults setFloat:[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] floatValue] forKey:@"version"];
}
}
和value1 2 3 4 5 6几乎都是相同的,但来自不同的plist,这里和示例
-(void)value1{
NSManagedObjectModel *mom = [self managedObjectModel];
NSDictionary *attrs = [[[mom entitiesByName] objectForKey:@"Hovedmenu"] attributesByName];
NSArray *keyedValues = [[NSArray alloc] initWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"hovedMenu" ofType:@"plist"]
];
for( NSDictionary *keyedValueDict in keyedValues ) {
NSManagedObject *mObj = [NSEntityDescription insertNewObjectForEntityForName:@"Hovedmenu" inManagedObjectContext:[self managedObjectContext]];
for (NSString *attribute in attrs) {
id value = [keyedValueDict objectForKey:attribute];
if (value == nil) {
// Don't attempt to set nil, or you'll overwite values in self that aren't present in keyedValues
continue;
}
NSAttributeType attributeType = [[attrs objectForKey:attribute] attributeType];
if ((attributeType == NSStringAttributeType) && ([value isKindOfClass:[NSNumber class]])) {
value = [value stringValue];
} else if (((attributeType == NSInteger16AttributeType) || (attributeType == NSInteger32AttributeType) || (attributeType == NSInteger64AttributeType) || (attributeType == NSBooleanAttributeType)) && ([value isKindOfClass:[NSString class]])) {
value = [NSNumber numberWithInteger:[value integerValue]];
} else if ((attributeType == NSFloatAttributeType) && ([value isKindOfClass:[NSString class]])) {
value = [NSNumber numberWithDouble:[value doubleValue]];
}
[mObj setValue:value forKey:attribute];
NSLog(@"Value %@ for Key %@", value, attribute);
}
}
NSError *error;
[[self managedObjectContext] save:&error];
}
在我的viewcontroller.h中
-(void)viewWillAppear:(BOOL)animated{
[self.mainTableView reloadData];
[super viewWillAppear:animated];
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
}
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
_fetchedResultsController = [Hovedmenu fetchAllGroupedBy:nil withPredicate:nil sortedBy:@"numberRow" ascending:YES delegate:self];
return _fetchedResultsController;
}
我也在使用MagicalRecord
答案 0 :(得分:1)
最相似的是,从我的代码中我可以看到,你用来获取value1,value2等的值的上下文与MagicalRecord为你创建的上下文不同。如果您更改该上下文以使用MR_defaultContext,则可能会正确更新所有内容。