2

CUSTOM_VIEW CLASS:

I have made custom_view class which calculates value on itself and shows to user after every 1 sec. Calculation of values in custom_view based on properties/variable stored in custom_view instance.

VIEWCONTROLLER CLASS:

I display some 7 to 9 view's by creating instance of custom_class in VIEWCONTROLLER class.

As my custom_class shows new calculated value after every 1 sec, i have used dispatch_async to execute the code of calculation. So that it won't affect UI Thread.

custom_view.m

   static dispatch_queue_t queue;
    queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0);

        dispatch_async(queue, ^(void)
                       {
                            [self calculateViewValue];
                       });



-(void) calculateViewValue
{
int wait = [self generateRandomNumberWithlowerBound:10 upperBound:20];

                              for (int i = 0; i<= wait; i++)
                              { 
                              // value calculation

                                     [[NSOperationQueue mainQueue] addOperationWithBlock:^
                                     {custom_view_instance.text = value;}];

                               sleep(1);
                              }
}

However, After executing it iPhone heats up after some time!! Am i doing something wrong / missing / best way to do it ???

nikdange_me
  • 2,949
  • 2
  • 16
  • 24

1 Answers1

2

do not make calculation in Views, Controllers do it. anyway do not call sleep in UIKit. a better approach can be: (code should be in a controller.. AND it sets text in views...) if you need to repeat calculations, use a timer.

So start with a code similar to:

uint64_t interval  = 1;
uint64_t leeway = 0;

dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, interval * NSEC_PER_SEC, leeway * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{

    // put code here...
});
dispatch_resume(timer);

some advantages: 1) lower cpu process 2) no sleep 3) already async. 4) you can use every "fire" of time to schedule an event 5) use a "count" var to decide when to sop timer.: in that case kill timer using something similar to dispatch_cancel... (saving your "timer")

ingconti
  • 10,876
  • 3
  • 61
  • 48
  • hi @ingconti, thanks for reply :) Well, as you mentioned i agree to your that calculation be done in controller, However calculation are made value stored in my custom_view class. Eg. Consider custom_view represents PROPERTY [HOUSE/BUILDING] and value on custom_view is RATE of property. so it value is based on properties/variable of custom_view. Therefore, i alway create a instance of custom_view and initialise every new custom_view with value. – nikdange_me Nov 01 '16 at 17:18
  • Views should not hold data or do calculations. Data should be in the model and calculations somewhere in model or control layer (depending on implementation details). – bbum Nov 01 '16 at 17:49
  • ok @bbum, yes i do prefer not use view for calculation / holding data which i dint do this time and will make change as you guys emphasise. Thanks :) Im being newbie, what should i refer / learn to avoid making any such mistake you pointed out and make more structured implementation in future. – nikdange_me Nov 01 '16 at 18:26
  • @nikdange_me. Part of the issue with your code is that it seems you are doing all these dispatches on a per view basis but, ultimately, the views are rendering data from a single model layer. Better would be to have multiple controls -- one per view "port" -- that then does the time based calculation (without sleep) based on the data fed by the model layer. – bbum Nov 01 '16 at 20:08