Changed the main thread signaling to use NSObject's performSelectorOnMainThread: functionality, which settles that. NSString requires an autorelease pool; in testing, so does NSValue, which we are eventually going to use. NSAutoreleasePool's docs suggest we should create a temporary pool for things running in other threads, but then we have to release it... but I guess we're waiting for the function to complete on other platforms already, so no big deal here.

This commit is contained in:
Pietro Gagliardi 2014-02-28 17:20:22 -05:00
parent 6982912a58
commit 56a436bc80
2 changed files with 10 additions and 32 deletions

View File

@ -51,14 +51,9 @@ func buttonClicked(self C.id, sel C.SEL, sender C.id) {
} }
//export gotNotification //export gotNotification
func gotNotification(self C.id, sel C.SEL, note C.id) { func gotNotification(self C.id, sel C.SEL, object C.id) {
data := C.objc_msgSend_noargs(note,
sel_getUid("userInfo"))
val := C.objc_msgSend_id(data,
sel_getUid("objectForKey:"),
notekey)
source := (*C.char)(unsafe.Pointer( source := (*C.char)(unsafe.Pointer(
C.objc_msgSend_noargs(val, C.objc_msgSend_noargs(object,
sel_getUid("UTF8String")))) sel_getUid("UTF8String"))))
fmt.Println("got notification from %s", fmt.Println("got notification from %s",
C.GoString(source)) C.GoString(source))

View File

@ -28,6 +28,7 @@ import (
// id objc_msgSend_id_sel_id_id(id obj, SEL sel, id a, SEL b, id c, id d) { return objc_msgSend(obj, sel, a, b, c, d); } // id objc_msgSend_id_sel_id_id(id obj, SEL sel, id a, SEL b, id c, id d) { return objc_msgSend(obj, sel, a, b, c, d); }
// id objc_msgSend_id_id_id(id obj, SEL sel, id a, id b, id c) { return objc_msgSend(obj, sel, a, b, c); } // id objc_msgSend_id_id_id(id obj, SEL sel, id a, id b, id c) { return objc_msgSend(obj, sel, a, b, c); }
// id objc_msgSend_id_id(id obj, SEL sel, id a, id b) { return objc_msgSend(obj, sel, a, b); } // id objc_msgSend_id_id(id obj, SEL sel, id a, id b) { return objc_msgSend(obj, sel, a, b); }
// id objc_msgSend_sel_id_bool(id obj, SEL sel, SEL a, id b, BOOL c) { return objc_msgSend(obj, sel, a, b, c); }
// Class NilClass = Nil; /* for newtypes.go */ // Class NilClass = Nil; /* for newtypes.go */
// id Nilid = nil; // id Nilid = nil;
import "C" import "C"
@ -49,8 +50,7 @@ func sel_getUid(sel string) C.SEL {
var NSApp C.id var NSApp C.id
var defNC C.id var defNC C.id
var delegate C.id var delegate C.id
var note C.id var notesel C.SEL
var notekey C.id
func init() { func init() {
// need an NSApplication first - see https://github.com/TooTallNate/NodObjC/issues/21 // need an NSApplication first - see https://github.com/TooTallNate/NodObjC/issues/21
@ -70,19 +70,7 @@ func init() {
objc_getClass("hello"), objc_getClass("hello"),
alloc) alloc)
noteStr := []C.char{'g', 'o', 'n', 'o', 't', 'e', 0} notesel = selN
note = C.objc_msgSend_strarg(
objc_getClass("NSString"),
sel_getUid("stringWithUTF8String:"),
&noteStr[0])
notekey = note
C.objc_msgSend_id_sel_id_id(
defNC,
sel_getUid("addObserver:selector:name:object:"),
delegate,
selN,
note,
C.Nilid)
} }
const ( const (
@ -114,17 +102,12 @@ func notify(source string) {
objc_getClass("NSString"), objc_getClass("NSString"),
sel_getUid("stringWithUTF8String:"), sel_getUid("stringWithUTF8String:"),
csource) csource)
dict := C.objc_msgSend_id_id( C.objc_msgSend_sel_id_bool(
objc_getClass("NSDictionary"), delegate,
sel_getUid("dictionaryWithObject:forKey:"), sel_getUid("performSelectorOnMainThread:withObject:waitUntilDone:"),
notesel,
src, src,
notekey) C.BOOL(C.FALSE)) // don't wait; we're using a channel for this (in the final ui code)
C.objc_msgSend_id_id_id(
defNC,
sel_getUid("postNotificationName:object:userInfo:"),
note,
C.Nilid,
dict)
} }
func main() { func main() {