Skip to content
This repository has been archived by the owner on Mar 7, 2021. It is now read-only.

Modifying a 'FORMFields' within 'self.dataSource.allFields' doesn't update its respective 'self.dataSource.values' #563

Open
sedwo opened this issue Jan 27, 2017 · 7 comments

Comments

@sedwo
Copy link

sedwo commented Jan 27, 2017

My challenge is that when I update a value like this:

FORMFields *formField = [self.dataSource.allFields objectForKey:stringKey];
formField.value = newStringValue;

It doesn't reflect that updated new value within the self.dataSource.values container.

As I need to update fields within a form that haven't yet been scrolled into view. And if they never are, then the data that gets saved is missing the updated new value.

I've hacked it by

NSMutableDictionary *updatedValues = [[NSMutableDictionary alloc] 

FORMFields *formField = [self.dataSource.allFields objectForKey:stringKey];
formField.value = newStringValue;

updatedValues[formField.fieldID] = formField.value;
[self.dataSource updateValuesWithDictionary:updatedValues];

But I would expect this to be embedded within the control. It looks ugly to wrap each modification like this.

Any other clever techniques on how to accomplish this ?

@3lvis
Copy link
Owner

3lvis commented Jan 27, 2017

Hi @sedwo,

What about doing:

[self.dataSource updateValuesWithDictionary:@{stringKey: newStringValue}];

Or could you share a bit more of your JSON structure? I think I could share an example on how to do this if I get some more info.

@sedwo
Copy link
Author

sedwo commented Jan 27, 2017

Are you suggesting then:

FORMFields *formField = [self.dataSource.allFields objectForKey:stringKey];
formField.value = newStringValue;    // update field
[self.dataSource updateValuesWithDictionary:@{stringKey: newStringValue}];    // update value

My thought (and hope) was that formField.value would update both. : )

Currently I'm experimenting with this snippet from other comments:

- (void)updateSelectField:(FORMFields *)formField withValue:(id)fieldValue
{
    __weak typeof(self)weakSelf = self;

    [self.dataSource fieldWithID:formField.fieldID
           includingHiddenFields:YES
                      completion:^(FORMFields *field, NSIndexPath *indexPath) {
         if (field)
         {
             field.value = fieldValue;
             [weakSelf.dataSource updateValuesWithDictionary:@{ field.fieldID : fieldValue }];
             [weakSelf.dataSource reloadFieldsAtIndexPaths:@[indexPath]];
         }
     }];
}

Does that method make sense?

@3lvis
Copy link
Owner

3lvis commented Jan 27, 2017

@sedwo what type of field is the one you're trying to update? https://github.com/hyperoslo/Form/blob/master/Source/Models/FORMField.h#L10-L24

@sedwo
Copy link
Author

sedwo commented Jan 27, 2017

At the moment just TEXT and DATE/TIME strings.

@sedwo
Copy link
Author

sedwo commented Jan 28, 2017

I added the method:

- (void)updateSelectField:(FORMFields *)formField withValue:(id)fieldValue
{
    __weak typeof(self)weakSelf = self;

    [self fieldWithID:formField.fieldID includingHiddenFields:YES completion:^(FORMFields *field, NSIndexPath *indexPath) {
        if (field)
        {
            field.value = fieldValue;
            [weakSelf updateValuesWithDictionary:@{ field.fieldID : fieldValue }];
            [weakSelf reloadFieldsAtIndexPaths:@[indexPath]];
        }
    }];
}

to the FORMDataSource class and it seems to do the job.

Thank you kindly for your feedback.

@sedwo sedwo closed this as completed Jan 28, 2017
@3lvis
Copy link
Owner

3lvis commented Jan 28, 2017

Gonna add a recommendation to the readme.

@3lvis 3lvis reopened this Jan 28, 2017
@sedwo
Copy link
Author

sedwo commented Feb 16, 2017

fyi
After further usage and testing, I simplified the method to simply be:

- (void)updateSelectField:(FORMFields *)formField withValue:(id)fieldValue
{
    if (formField && fieldValue != nil)
    {
        formField.value = fieldValue;
        [self updateValuesWithDictionary:@{ formField.fieldID : fieldValue }];
    }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants