简体   繁体   English

如何更新核心数据中的可变形属性

[英]How to update transformable attribute in core data

I am in a situation where i need to update transformable attribute in my entity in core data, until now i've tried every possible answer from google and stack overflow but did't achieve anything. 我处于一种需要在核心数据中更新实体中的transformable属性的情况下,直到现在我已经尝试过Google的所有可能答案和堆栈溢出,但没有实现任何目的。

This is the method where i am saving object in core data, and my object which i am saving is an NSMutablDictionary type object. 这是我在核心数据中保存对象的方法,而我要保存的对象是NSMutablDictionary类型的对象。

-(void)didSaveToCoreData :(NSMutableDictionary *)newDict
{

@try {
    AppDelegate *appDelegate  = [[UIApplication sharedApplication]delegate];

    NSManagedObjectContext *context = appDelegate.managedObjectContext ;

    DataModelSupport *entity = [NSEntityDescription insertNewObjectForEntityForName:@"CPIEntity" inManagedObjectContext:context];


    if (newDict != nil) {

        [entity.fixed_Model removeAllObjects];
        entity.fixed_Model = newDict;
    }

    NSError *error ;
    [context save:&error];
    if(error)
    {
        NSLog(@"Error in Saving Data");
    }
    else
    {
        [self didFetchFromCoreDataModel];
        NSLog(@"Successfully saved");
    }

}
@catch (NSException *exception) {
    [self spareMeFromTheCrash:exception];
}
@finally {

}

}

in this method i am saving a dictionary object of 19 key/value, at the first time and i am fetching it correctly in didFetchFromCoreDataModel method, but when i refresh the data and get dictionary of 18 key/value i save that dictionary in core data using the same method didSaveToCoreData and fetch it in the same way from didFetchFromCoreDataModel but it still show 19 key/value 在这种方法中,我第一次保存一个19键/值的字典对象,并在didFetchFromCoreDataModel方法中正确获取它,但是当我刷新数据并获取18键/值的字典时,我将该字典保存在核心数据中使用相同的方法didSaveToCoreData并以相同的方式从didFetchFromCoreDataModel获取它,但它仍显示19键/值

DataModelSupport is a subclass of NSManagedObject. DataModelSupport是NSManagedObject的子类。 In DataModelSupport.h: 在DataModelSupport.h中:

@property (nonatomic,weak) NSMutableDictionary *fixed_Model;

In DataModelSupport.m: 在DataModelSupport.m中:

@dynamic fixed_Model;

This is it for the DataModelSupport class. 这就是DataModelSupport类。

Now here in this method i am fetching the same object form core data 现在,在此方法中,我正在从核心数据中获取相同的对象

-(void)didFetchFromCoreDataModel
{

@try {
    AppDelegate *appDelegate  = [[UIApplication sharedApplication]delegate];

    NSManagedObjectContext *context = appDelegate.managedObjectContext ;
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"CPIEntity" inManagedObjectContext:context];

    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    [request setReturnsDistinctResults:YES];
    [request setReturnsObjectsAsFaults:NO];
    [request setResultType:NSDictionaryResultType];
    [request setEntity:entity];
    NSError *error ;

    NSArray *arr = [context executeFetchRequest:request error:&error];

    updatedfinalArr = [arr valueForKey:@"fixed_Model"];



    if(error)
    {
        NSLog(@"Error");
    }
    else
    {

    }

}
@catch (NSException *exception) {
    [self spareMeFromTheCrash:exception];
}
@finally {

}

}

And this is how my core data looks like:- 这就是我的核心数据的样子:- 在此处输入图片说明

Any help is appreciated. 任何帮助表示赞赏。

EDIT 编辑

I've implemented some changes in my code now in didSaveToCoreData method i am using this line of code to fetch the Entity by name 我现在使用didSaveToCoreData方法在代码中实现了一些更改,我正在使用此行代码按名称获取Entity

NSEntityDescription *descriptor = [NSEntityDescription entityForName:@"CPIEntity" inManagedObjectContext:context];

by this i am not creating new entity every time i call didSaveToCoreData method. 因此,我不会在每次调用didSaveToCoreData方法时创建新实体。

and this is how i am saving NSMutlableDictionary object 这就是我保存NSMutlableDictionary对象的方式

DataModelSupport *entity = [[DataModelSupport alloc]initWithEntity:descriptor insertIntoManagedObjectContext:context];
[entity.fixed_Model removeAllObjects]
entity.fixed_Model = newDict;

but still i am not getting correct result. 但是我仍然没有得到正确的结果。 now when i refresh the data and save it using the above procedure explained in EDIT section, and fetch it, i get the updated data but it increase the number of objects, like on first attempt when i fetch i got 1 object in array, and on second attempt i got 2 objects and it goes like this, so when ever new data is added its not updating it but instead it add it in the entity s fixed_Model attribute and increase the number of object. 现在,当我刷新数据并使用“编辑”部分中说明的上述过程将其保存并获取时,我获取了更新的数据,但它增加了对象数,就像第一次尝试获取数组中有1个对象时一样,并且在第二次尝试中,我得到了2个对象,并且它是这样的,所以当添加新数据时,它不会更新它,而是将其添加到实体的fixed_Model属性中,并增加了对象数。

Lastly now i am using this line of code to get the last and update object from array in didFetchFromCoreDataModel method 最后,现在我使用这一行代码从didFetchFromCoreDataModel方法中的数组中获取最后一个并更新对象

NSDictionary *ddd = [[arr valueForKey:@"fixed_Model"]lastObject];
updatedfinalArr = [NSMutableArray arrayWithObject:ddd];

Your save method creates a new CPIEntity object each time. 您的save方法每次都会创建一个新的CPIEntity对象。 So, unless you delete the old object elsewhere in your code, I suspect your fetch is returning several objects, the first of which has the dictionary with 19 key/value pairs in the fixed_Model attribute, and the second/subsequent objects contain the 18 key/value pairs. 因此,除非您在代码中的其他位置删除了旧对象,否则我怀疑您的访fixed_Model将返回几个对象,其中第一个对象在fixed_Model属性中具有包含19个键/值对的fixed_Model ,第二个/后续对象包含18个键/值对。

When you save, you should try to fetch the existing object first, and if you get zero results then create a new object. 保存时,应首先尝试获取现有对象,如果结果为零,则创建一个新对象。 Then set the fixed_Model attribute of the new/existing object to your new dictionary. 然后将新对象/现有对象的fixed_Model属性设置为新字典。

EDIT 编辑

You are still inserting a new object each time ( DataModelSupport *entity = [[DataModelSupport alloc]initWithEntity:descriptor insertIntoManagedObjectContext:context]; ). 每次仍在插入新对象( DataModelSupport *entity = [[DataModelSupport alloc]initWithEntity:descriptor insertIntoManagedObjectContext:context]; )。 See below for an example of "fetch or create": 请参阅下面的“获取或创建”示例:

AppDelegate *appDelegate  = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = appDelegate.managedObjectContext ;
NSEntityDescription *descriptor = [NSEntityDescription entityForName:@"CPIEntity" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
request.entity = descriptor;
NSError *error;
NSArray *results = [context executeFetchRequest:request error:&error];
if (results == nil) {
    // This implies an error has occurred.
    NSLog(@"Error from Core Data: %@", error);
} else {
    if (results.count == 0) {
        // No objects saved, create a new one...
        DataModelSupport *entity = [[DataModelSupport alloc]initWithEntity:descriptor insertIntoManagedObjectContext:context];            
        entity.fixed_Model = newDict;
    } else {
        // At least one object saved.  There should be only one
        // so use the first... 
        DataModelSupport *entity = [results firstObject];
        entity.fixed_Model = newDict;
    }
}

I've assumed for simplicity that newDict is not nil; 为简单起见,我假设newDict不为nil; amend as appropriate to handle that case. 进行适当修改以处理这种情况。

Can you narrow down the problem? 您可以缩小问题范围吗?

Ie. IE浏览器。 can you compare the two Dictionaries..the original one with 19 values and the new one with 18 values? 您可以比较两个Dictionary ..原始的19个值和新的18个值吗?

Is there a particular entry which is not being 'removed'? 是否有没有被“删除”的特定条目? That might point to a challenge with 'delete' (or the lack there of). 这可能意味着“删除”(或缺少)的挑战。

Alternatively, if you completely replace the content, what result do you get on fetch? 或者,如果您完全替换了内容,则获取时会得到什么结果?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM