2014-01-21 108 views
0

我想从plist文件中的数据的UITableView填充到核心数据核心数据,在第一次运行时将不会显示在UITableView的,第二次运行数据确定

这让我很容易可以更新在以后的版本中的UITableView ,那么我只需要改变plist的生活方式,核心数据就可以处理剩下的事情。请告诉我是否有更好的方法...

无论如何,我在这里做的工作不正常,在第一次运行它填充核心数据,但UITableView是空的,那么如果你关闭应用程序完全,也从多任务栏啄并启动它再次,现在显示的数据

在我的应用程序delegate.h的UITableView的我:

- (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"]; 
} 
} 

和值1 2 3 4 5 6几乎都是一样的,但来自不同的plists,这里是和例

-(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以及

回答

1

最LIKEY,从我可以从你的代码在这里看到的是,上下文你是哪个用于获取value1,value2等的值与MagicalRecord为您创建的上下文不同。如果您更改该上下文以使用MR_defaultContext,则可能会正确更新所有内容。

+0

[NSManagedObjectContext(MagicalSaves)MR_saveWithOptions:completion:](0x8c944e0)** DEFAULT **中没有任何变化上下文 - 不保存 – KennyVB

+0

难道是神奇的记录是“慢”吗? – KennyVB

+0

让它工作。只是改变了你所说的,只要我能够做到,现在就是探索者的更新 – KennyVB

相关问题