0

I am using PromiseKit (awesome framework, btw!) to handle communication between my app and a server API. I would like to create some kind of global handler to respond to things like notifying the user about a lack of network connection across any of my many promises.

The PMKPromise.h file lists an unhandled error handler:

/**
Called by PromiseKit in the event of unhandled errors.
The default handler NSLogs the error. Note, your handler is executed
from an undefined queue, unless you manage thread-safe data, dispatch to
a safe queue before doing anything else in your handler.
*/
extern void (^PMKUnhandledErrorHandler)(NSError *);

This type is then implemented in the PMKPromise.m file, though the doc block implies that this could be overwritten. My question is how exactly do I redefine this variable?

Copying the same syntax as used in the .m file gives me a linker error

void(^PMKUnhandledErrorHandler)(id) = ^(NSError *error){
    //...
};

==> duplicate symbol _PMKUnhandledErrorHandler
Sean Fraser
  • 342
  • 3
  • 14
  • are you trying to redefine the block type or create a block of that type? Also, what scope is your second code block in? Top level? Inside a function or method? – Patrick Goley Apr 15 '15 at 16:16
  • trying to redefine the type. Was trying to redefine at the global scope of one of my .m files. – Sean Fraser Apr 15 '15 at 16:28

1 Answers1

1
PMKUnhandledErrorHandler = ^(id error) {
    // your code here
};

Be wary of the unhandier error handler, sometimes it gets called a noticeable amount of time after the error occurred, depending on when the promise in question is deallocated. It is reliable, but not always immediately reliable.

99% of the time it would be slow because some object has a handle on a promise somewhere in your chain still and it soon to be deallocated but is not yet deallocated.

mxcl
  • 26,392
  • 12
  • 99
  • 98
  • Thank you, that worked. One thing to note it this seems to need to be used within a method. Using it globally causes a type error. – Sean Fraser Apr 21 '15 at 13:52