【问题标题】:iCloud, Core Data and duplicates and how to seed initial data?iCloud、Core Data 和副本以及如何播种初始数据?
【发布时间】:2014-01-23 11:23:35
【问题描述】:

我想检查 iCloud 中是否有数据,如果没有,则为其播种新数据并填充它。如果那里有数据,请什么也不做。如果它可以与版本控制一起使用。

我有办法在不启用 iCloud 的情况下做到这一点,但我真的想将 iCloud 集成到我的应用程序中。到目前为止,每当我更改应用程序的版本时,我都会得到重复,因为它会运行新版本控制的代码

这就是我在没有 iCloud 的情况下的做法。

所以我的问题是它如何检查 iCloud 中是否有任何数据,以及它是否从未使用过。如果它较旧,请重新编写它或类似的东西。只是一种在我对 hovedmenu 中的 plist 进行更改并使用它时播种数据的方法。

仅供参考,我正在使用 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 文档。 ossh.com.au/design-and-technology/software-development/…
  • 通过在 iCloud 容器 /CoreData 目录中查找与 NSPersistentStoreUbiquityNameKey 同名的目录来完成 iCloud 检查。如果设备未登录 iCloud 或自另一台设备在 iCloud 中创建商店后没有网络访问权限,则此检查将不起作用。

标签: ios core-data icloud magicalrecord


【解决方案1】:

观看来自 WWDC 2012 的视频Using iCloud with Core Data。他们还解释了如何完全按照您的意愿去做,并指出了有关 iCloud 的其他材料。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    相关资源
    最近更新 更多