iCloud,核心数据和重复以及如何为初始数据提供种子?

时间:2014-01-23 11:23:35

标签: ios core-data icloud magicalrecord

我想检查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);
    }

}
}

1 个答案:

答案 0 :(得分:0)

从WWDC 2012 Using iCloud with Core Data查看此视频。他们还解释了如何做到你想要的,并指出其他有关iCloud的材料。