On Fri, Jul 11, 2008 at 9:30 AM, Jerry Isdale <[EMAIL PROTECTED]> wrote:
> I have an application whose data model (classes, shared Instances, etc) gets
> updated by various threads for calculations, network comms, etc.
> Some of the application's Views use binding and KVO.  For example an
> NSTextField may get bound to SharedInstance.currentLattitude
>
> It the property gets updated using KVO compliant code, on what thread is the
> NSTextField updated?

On the same thread as the property was set (i.e. probably not what you
want, as most views and controllers need to do their work on the main
thread).


>
> Possible answers include:
> * thread that invokes the set method

This.

> * MainThread - because all UI updates should happen there.

Not This :)


Essentially, you will have to make sure that all properties that are
being observed by views and their controllers happen on the main
thread. A category such as the following has helped me in the past in
such situations (warning, typed up from memory in my mail window):

@implementation NSObject(CSCAdditions)

- (void)CSCSetValueForKeyImpl:(NSDictionary*)dict {
    [self setValue: [dict objectForKey: @"value"] forKeyPath: [dict
objectForKey: @"key"]];
}

- (void)CSCSetValueForKeyPathImpl:(NSDictionary*)dict {
    [self setValue: [dict objectForKey: @"value"] forKeyPath: [dict
objectForKey: @"keyPath"]];
}

- (void)setValuesOnMainThreadForKeysWithDictionary:(NSDictionary
*)dict waitUntilDone:(BOOL)wait {
    [self performSelectorOnMainThread:
@selector(setValuesForKeysWithDictionary:) withObject: dict
waitUntilDone: wait]
}

- (void)setValueOnMainThread:(id)value forKey:(NSString *)key
waitUntilDone:(BOOL)wait {
        NSParameterAssert(key != nil);
        
    [self performSelectorOnMainThread: @selector(CSCSetValueForKeyImpl:)
                                           withObject: [NSDictionary
dictionaryWithObjectsAndKeys: keyPath, @"key", value, @"value", nil]
                                        waitUntilDone: wait];
}

- (void)setValueOnMainThread:(id)value forKeyPath:(NSString *)keyPath
waitUntilDone:(BOOL)wait {
        NSParameterAssert(keyPath != nil);
        
    [self performSelectorOnMainThread: @selector(CSCSetValueForKeyPathImpl:)
                                           withObject: [NSDictionary
dictionaryWithObjectsAndKeys: keyPath, @"keyPath", value, @"value",
nil]
                                        waitUntilDone: wait];
}

@end

Then, you just have to make sure that any modifications to properties
that happen on a background thread go through one of these three
"-set...onMainThread:..." methods.

-- 
Clark S. Cox III
[EMAIL PROTECTED]
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to