开发者

Very odd behaviour by Core Data and NSArrayController

I've a document-based application powered by Core Data with an in-memory store. I have a table backed by an NSArrayController that is suppose to list all model objects of type Buffer. My UI also includes an NSTextView that fetches data from the the currently selected Buffer object.

I try to populate the text view thus (I'm using Fragaria):

- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
    if ([aNotification object] == editorList) {
        Buffer *buffer = [[editorListArrayController selectedObjects] objectAtIndex:0];
        [fragaria setString:[buffer valueForKey:@"content"]];
    }
}

Now, whenever the user types something into the text view, I save t开发者_运维问答hat into the currently selected buffer and then save the changes to the managed object context:

- (void)textDidChange:(NSNotification *)notification
{
    Buffer *buffer = [[editorListArrayController selectedObjects] objectAtIndex:0];
    [buffer setValue:[fragaria string] forKey:@"content"];
    [[self managedObjectContext] saveChanges];
    [self setEditedFlagForModelAndWindow:YES];
}

My problem is that when I list all the model objects in my NSArrayController they all seem to have the same value for @"content", meaning that the same value is somehow being written to all model objects. How can I debug this?


Fortunately i hade the exact same problem some time ago.

The problem is your assignment:

[buffer setValue:[fragaria string] forKey:@"content"];

[fragaria string] returns a pointer to a mutable string. Every edit in your NSTextView updates the same NSMutableString object resulting in all Buffer objects being changed without Notification.

The following code works:

NSString *result = [NSString stringWithFormat:@"%@", [fragaria string]];
[buffer setValue:result forKey:@"content"];

Also [NSString stringWithString:] could be enough but I did not check this yet.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜