Wait for Obj-C delegate method to be called in Obj-C or C++

Discussion in 'Mac Programming' started by moonman239, Jun 28, 2015.

  1. moonman239 macrumors 68000

    Mar 27, 2009
    My goal is to create a Python module for document scanning. What I want to do is allow a Python program to grab a list of scanner devices and present them to the user. Unfortunately, ICDeviceBrowser uses delegation to post whatever scanner devices it sees. I believe people who use my Python module to find scanners will want to be able to call a Python function that returns the list. So, I believe that means I need to create a C++ function that does exactly that.

    Since ICDeviceBrowser's delegate protocol's didAddDevice method has a moreComing BOOL, I'm thinking of giving my C++ class a moreComing bool property and having it run a loop until that bool is set to "true"
  2. whooleytoo macrumors 603


    Aug 2, 2002
    Cork, Ireland.
    I think wrapping an asynchronous (non-blocking) call in a synchronous (blocking) call is rarely a good idea.

    There's typically a good reason why the asynchronous call is asynchronous in the first place - usually because it takes some time to execute and would otherwise block that thread. If you wrap that call in a loop that could block the thread too.

    If possible, could you try pre-fetching the list of devices when your module is initialised and caching that list, so when the function to list them is called you can return it immediately? It might mean the device list is slightly out of date, but perhaps that's a worthwhile compromise for not blocking the thread.

Share This Page