1
我想检查是否有数据在iCloud中如果没有任何种子新数据并填充它。如果有数据在那里使用什么也不做。如果它可以与版本控制一起工作。iCloud,Core Data和重复项以及如何种子初始数据?
我有办法做到这一点没有启用iCloud,但我真的想在我的应用程序中集成iCloud。截至目前,当我更改应用程序的版本时,我会得到重复内容,因为它会运行新版本的代码
以下是我如何在没有iCloud的情况下执行此操作。
所以我的问题是,它是如何检查是否有任何数据在iCloud中,如果它从来没有使用过。如果它年纪大了,重新写它或类似的东西。只是在我修改hovedmenu中的plist并使用该数据时种下我的数据。
FYI即时期运用MagicalRecord但我可以和正常将使用标准的核心数据代码,以填补它
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (![userDefaults valueForKey:@"version"])
{
[Hovedmenu MR_truncateAll];
[self hovedMenu];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
NSLog(@"running code");
// 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];
[self hovedMenu];
NSLog(@"running code agian");
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
// Update version number to NSUserDefaults for other versions:
[userDefaults setFloat:[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] floatValue] forKey:@"version"];
}
NSLog(@"%@", [NSString stringWithFormat:@"Version %@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]]);
-(void)hovedMenu{
NSManagedObjectModel *mom = [NSManagedObjectModel MR_defaultManagedObjectModel];
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:[NSManagedObjectContext MR_defaultContext]];
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);
}
}
}
看看这里的例子。我发布了一个示例应用程序,首先检查在设备上初始化新商店时是否存在iCloud文档。 http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/ –
iCloud检查是通过查找目录,其名称与iCloud容器'/ CoreData'目录中的'NSPersistentStoreUbiquityNameKey'同名。如果设备未登录到iCloud或由于其他设备在iCloud中创建了商店而无法访问网络,此检查将不起作用。 –