开发者

what is wrong with this UITableView code, where I'm using pass-by-reference to update a core data config record?

开发者 https://www.devze.com 2023-04-10 19:33 出处:网络
I have core data configuration in my iPhone app, and how I\'m currently passing the configuration to the view that allows a user to EDIT the data, is via passing the core data object by reference.In f

I have core data configuration in my iPhone app, and how I'm currently passing the configuration to the view that allows a user to EDIT the data, is via passing the core data object by reference. In fact I'm allocated the core data class attribute (NSString) to the "UITextField.text" in the edit view, so that if it gets updated then it is effectively updating the core data object. The issue is:

  1. it seems to work fine when I create/update this string in the edit view
  2. when I update a separate field (not the one in question) then the value seems to be null

Question - Is there anything fundamentally wrong with this pass-by-reference approach using a core-data managed object attribute?

Code snippets:

a) In the Edit Controller - in cellForRowAtIndexPath

if (indexPath.row == 0) {
        // Setup
    UITextField *titleTextField = nil;
    // Get Cell
    self.nonWorkTermCell = [tableView dequeueReusableCellWithIdentifier:@"nonWorkTermCell"];
    if (self.nonWorkTermCell == nil) {
        self.nonWorkTermCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"nonWorkTermCell"] autorelease];
            self.nonWorkTermCell.textLabel.text = @"Non Work Term:";

            titleTextField = [[self newTextFieldForCell:self.nonWorkTermCell] autorelease];
            titleTextField.keyboardType = UIKeyboardTypeURL;
            titleTextField.returnKeyType = UIReturnKeyDone; 
        titleTextField.delegate = self;  // TODO: Is this needed at all?
        titleTextField.tag = 1;
            [self.nonWorkTermCell addSubview:titleTextField];
    }
    // Set Value
    titleTextField.text = self.weConfig.nonWorkTerm;   // ** ASSI开发者_如何学CGNS THE CORE DATA MANAGED OBJECT CONFIG ITEM DIRECTLY TO THE TEXT FIELD

        // Return 
    return self.nonWorkTermCell;

b) and the helper method it uses:

- (UITextField *)newTextFieldForCell:(UITableViewCell *)cell {
    UITextField *addTextField = [[UITextField alloc] initWithFrame:frame];  //cut the code for the frame to save space
    addTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    addTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    addTextField.delegate = self;
    addTextField.clearButtonMode = UITextFieldViewModeNever;
    addTextField.enabled = YES;
    addTextField.returnKeyType = UIReturnKeyDone;
    addTextField.clearButtonMode = UITextFieldViewModeWhileEditing;  
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return addTextField;
}

c) then back in the delegate

- (void)applicationWillResignActive:(UIApplication *)application {
    [self saveContext];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [self saveContext];
} 


Assigning a managed object's NSString property to a textField and the editing the textfield's content will not propagate those changes back to the managed object, for a number of reasons:

  • NSString is immutable, so by definition you won't be making changes to the same object
  • The object returned by an accessor on managed object could well be an autoreleased copy of the underlying value
  • The textfield also probably takes a copy of whatever value is assigned to it's text property
  • No changes will go back to the model unless they have passed through the managed object accessors which include the KVO notifications
  • The whole idea is insane. What if I wanted to take a string that I'd got from a managed object and perform some other operation on it for presentation or analysis?


It seems like passing a dynamic core data managed property by reference may not work....?

So this answer is (for voting/confirmation) that passing a dynamic core data managed property by reference doesn't work.

[I've changed to manually setting the model via use of "textFieldDidEndEditing" which seems to fix things]


Greg,

The line in question is:

titleTextField.text = self.weConfig.nonWorkTerm;

and weConfig is your managed object? and nonWorkTerm is an NSString?

If the answer to everything above is yes, then this is a supported assignment. (I'm not sure why you think this is a pass by reference. It looks like the value is being directly assigned to me. [Actually, titleTextField will most likely copy the string.])

What is the problem?

Andrew

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号