How to get the objects out from an array using completion handler.

Discussion in 'iOS Programming' started by thangiavel, Sep 8, 2011.

  1. thangiavel, Sep 8, 2011
    Last edited: Sep 8, 2011

    thangiavel macrumors newbie

    Joined:
    Sep 5, 2011
    #1
    i have an array of objects ..i need to use this array in another viewController named as test pickerViewController....for that i am using completion handler ....how can i use the completion handler in this testPicker viewcontroller to fill the object array with the testpickerViewcontroller's array...please could anyone help me for this ....help will be appriciated....i hope some one will help me to do this.....
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    I don't completely understand your question, but let's stick to this:
    YOu have an array you want to use in another class.
    Have u tried making it a Singleton yet? if not.. google SynthesizeSingleton.
    Then in your second viewController, load up your array from your singleton, and tadaa. you can fill it up there.
     
  3. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #3
    Singletons are rarely the best design choice, and in this case it doesn't sound necessary.

    I think we need to see some real code to figure out the best solution to this problem.
     
  4. Sykte, Sep 8, 2011
    Last edited: Sep 8, 2011

    Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #4
    Your other thread was locked.

    I'm not sure you fully understand blocks or I'm not understanding what you want. "completionHandler" is a block that is being passed as an argument. If you are passing the block into the init method then calling it later you need to make sure you are copying the block either via a property or doing a block copy and setting it to a private variable so it does not go out of scope. As far as the array goes that's only a matter of passing it in as an argument to the block. The "completionHandler" you refer to is a alternative to a delegate callback. If you're unsure what a delegate callback or a block is then you need to do further reading. Post more code with better context and hopefully we can help.

    On a site note, giving your objects a better naming convention \ more specific names will help other people help you. Having an array with a name of arrayForObject and another one named arrayObject can be confusing when it's not your code base.


    Edit: I had a few moments so I typed up an example I hope this helps.

    Code:
    //
    //  DJSFactory.h
    //
    
    #import <Foundation/Foundation.h>
    
    typedef void(^ myCustomCompletionHandler)(NSArray *factoryArray); //This is not required but I would prefer to work with a type especially when you have multiple arguments, you can declare the block inline if you want.
    
    @interface DJSFactory : NSObject
    
    @property(nonatomic, copy)myCustomCompletionHandler completionCallBack;//our block as a property
    @property(nonatomic, retain)NSArray *myFactoryArray; //misc data doesn't matter
    
    -(id)initWithCompletionHandler:(myCustomCompletionHandler)completionHandler; //our custom init method
    -(void)startReallyLongAsyncTaskHere; //stupid task doesn't really matter
    
    @end
    
    Code:
    //
    //  DJSFactory.m
    //
    
    #import "DJSFactory.h"
    
    @implementation DJSFactory
    @synthesize completionCallBack;
    @synthesize myFactoryArray;
    
    -(id)initWithCompletionHandler:(myCustomCompletionHandler)completionHandler {
        self = [super init];
        if (self) {
            self.completionCallBack = completionHandler; //we have a property copying the block
            [self startReallyLongAsyncTaskHere];
        }
        return self;
    }
    
    
    -(void)startReallyLongAsyncTaskHere {
        //we make a really large factory array here in the background
        //once we are ready to call our completionHandler
        self.completionCallBack(self.myFactoryArray);
    }
    
    
    
    @end
    
    
    Code:
    //
    //  ViewController.m
    //
    
    #import "ViewController.h"
    #import "DJSFactory.h"
    
    @implementation ViewController
    
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	
        DJSFactory *factoryObject = [[DJSFactory alloc] initWithCompletionHandler:^(NSArray *factoryArray) {
          //here we place what we want out of the factory array
         //we will do a simple log statement.
    
            NSString *myName = [factoryArray objectAtIndex:0];
            NSLog(@"%@", myName);
    
        //Side Note: if this was a true async call this may not return immediately, so the NSLog statement would  be delayed.
       //You can however use this synchronously and get an immediate call back.
        }];
    }
    
    @end
    
    This is a stupid example and typed by hand but hope it helps. No guarantee it will copy/paste properly.
     

Share This Page