编辑CoreData对象然后保存上下文

问题描述 投票:0回答:2

我有两个实体,一个称为InProject,具有多个属性和一个关系。关系与另一个称为Ins的实体。

我正在编辑与Ins相关的InProject之一。我使用了InProject属性ID,该属性随后返回一个NSDictionary值,该值具有多个键值,其中一个键值用于Ins数组。然后,我在for循环中找到了需要编辑的Ins,然后对其进行了编辑,但是由于我不确定如何使用* updated Ins

保存InProject的内容,因此我变得无法使用

[我需要弄清楚我需要更新的InProject属性后如何保存Ins

这是解决此问题后我的代码的样子:

- (void)editSelectedins:(NSString *)projIDString UpdatedNSD:(NSMutableDictionary *)updatedNSD DPC:(int)dpc{

        // get context
        NSManagedObjectContext *context = [self managedObjectContext];

        if (context == nil) {
            NSLog(@"Nil");
        }
        else {
            NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
            NSEntityDescription *entity = [NSEntityDescription entityForName:@"InsProject" inManagedObjectContext:context];
            [fetchRequest setEntity:entity];

            NSError *error;
            NSMutableArray *InsProjectDictionaryArray = [[NSMutableArray alloc] init];
            NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

            for (InsProject *insProj in fetchedObjects) {
                NSMutableDictionary *tempInsProjectDictionaryArray = [[ NSMutableDictionary alloc] init];

                [tempInsProjectDictionaryArray setObject:insProj.companyName forKey:@"CompanyName"];
                [tempInsProjectDictionaryArray setObject:insProj.projNo forKey:@"ProjNo"];
                [tempInsProjectDictionaryArray setObject:insProj.desc forKey:@"Desc"];
                [tempInsProjectDictionaryArray setObject:insProj.guid forKey:@"GUID"];
                [tempInsProjectDictionaryArray setObject:insProj.projID forKey:@"ProjID"];
                [tempInsProjectDictionaryArray setObject:insProj.ins forKey:@"ins"];

                [InsProjectDictionaryArray addObject:tempInsProjectDictionaryArray];
            }

            // now that you have the InsProjects, choose the one you are curently working on in insView using the projectID
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ProjID==%@",projIDString];
            [fetchRequest setPredicate:predicate];

            // new array with one value that was created using the NSPredicate ProjID
            NSArray *tempInsProjectArray = [InsProjectDictionaryArray filteredArrayUsingPredicate:predicate];

            // get ins array out of the NSDictionary to edit
            NSSet *inssForInsProject = tempInsProjectArray[0][@"ins"];
            NSMutableArray *tempAllinss = [[NSMutableArray alloc] init]; // this will contain everything, that means all repeated values are included

            for (Items* currItem in [inssForInsProject allObjects]) {
                NSArray *keys = [[[currItem entity] attributesByName] allKeys];
                NSDictionary *dict = [currItem dictionaryWithValuesForKeys:keys];
                [tempAllinss addObject:dict];
            }

            NSArray *myArray = [tempAllinss copy];

            // get the correct items from myArray anything whos dpc matches the dpc parameter of this method
            NSMutableArray *editedinsArray = [[NSMutableArray alloc] init];
            for (int i = 0; i < [myArray count]; i++) {
                NSMutableDictionary *tempinssDictionary = [myArray objectAtIndex:i];

                // if you get a match put it into the new editedinsArray to be edited
                if ([[tempinssDictionary objectForKey:@"dpc"] integerValue] == dpc) {
                    [editedinsArray addObject:tempinssDictionary];

                }
            }

            // by now you should have three things
            // 1, access to your ins coredata object //this s wrong I actually have access to insProject
            // 2, the values you need to be edited saved into a NSArray (editedinsArray, which will be used to check against and keep old values correct)
            // 3, UpdatedNSD which will be used to update any values that need to be updated.


            // go through your values and update the ins object
            int i = 0;
            for (ins *temp in editedinsArray) {
                NSDictionary *currentEditedins = [editedinsArray objectAtIndex:i];
                i++;

                // these values should stay the same so use currentEditedins which contains old vals
                NSString *stringToNumberDpc = [currentEditedins valueForKey:@"dpc"];
                int tempDpcNum = [stringToNumberDpc integerValue];
                NSNumber *dpcNumber = [NSNumber numberWithInt:tempDpcNum];
                temp.dpc = dpcNumber;

                NSString *totDQtyString = [currentEditedins valueForKey:@"totDQty"];
                if ((NSNull *)totDQtyString == [NSNull null]) {
                    temp.totDQty = @"";
                } else {
                    temp.totDQty = totDQtyString;
                }

                NSString *totShipString = [currentEditedins valueForKey:@"totShip"];
                if ((NSNull *)totShipString == [NSNull null]) {
                    temp.totShip = @"";
                } else {
                    temp.totShip = totShipString;
                }


                // values to be updated so use updatedNSD wthich was passed in as method param with the new vals
                temp.newInsComp = [updatedNSD valueForKey:@"newInsComp"];
                temp.newDryComp = [updatedNSD valueForKey:@"newDryComp"];
                temp.updatedRow = [updatedNSD valueForKey:@"updatedRow"];

            }
#warning --- I have no idea what to do here... i.e. how do I update the tempInsProjectArray.ins values I have just updated in the above for loop then save context which I hope would update insProj and the ins entities involved.         

            //save
            [context save:&error];


        }

}

您可以在代码底部使用#warning看到,我解释了问题所在。如果将temp记录在for循环中,则可以完美地看到更新后的值,我遇到的问题是如何更新刚刚编辑的当前tempInsProjectArray.ins值?然后保存它们。

我有两个实体,一个称为InProject,它具有多个属性和一个关系。关系是与另一个称为Ins的实体建立的。我正在编辑与...

ios core-data nsdictionary nsmutabledictionary nsset
2个回答
3
投票

您的代码非常需要简化。一些基本规则:


1
投票

如果您知道您的InProject,那么更新与该项目相关的Ins就是编辑托管对象上的属性值。

© www.soinside.com 2019 - 2024. All rights reserved.