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以及
[NSManagedObjectContext(MagicalSaves)MR_saveWithOptions:completion:](0x8c944e0)** DEFAULT **中沒有任何變化上下文 - 不保存 – KennyVB
難道是神奇的記錄是「慢」嗎? – KennyVB
讓它工作。只是改變了你所說的,只要我能夠做到,現在就是探索者的更新 – KennyVB