IOS - Objective-C - texts and images of widgets double - old data is not cleared

In iOS application widgets, I see only some devices, doubled data (see the figure below). I tried to determine the device, version of iOS, but it seems "random". In addition, I can’t debug this myself, because on each of my devices everything is displayed correctly and blind debugs do not work (several updates in the AppStore, but still with the same error).

In widgets, I download (in a background stream) new data from the Internet and put it (in dispatch_get_main_queue()) in tags, images, etc. Everything works fine, but sometimes old data is not "cleared". In my design file for the widget, I cleared all the texts by default, so this is not a problem.

enter image description here

Double icon and texts 4.1 ° C and 7.9 ° C overlap

The bulk of my widget code (shortened by deleting other labels, tables, and geolocation):

- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([self.extensionContext respondsToSelector:@selector(widgetLargestAvailableDisplayMode)])
    {
        //this is iOS >= 10                
        self.extensionContext.widgetLargestAvailableDisplayMode = NCWidgetDisplayModeExpanded;
    }


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(FinishDownload:) name:@"FinishDownload" object:nil];


    self.preferredContentSize = CGSizeMake(320, 160);

     [self updateData];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self updateData];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self updateData];
}

-(void)updateData
{

    [[[DataManager SharedManager] settings] Reload];


    [[CoreDataManager SharedManager] reset];

    if ([[DataManager SharedManager] DownloadDataWithAfterSelector:@"FinishDownload"] == NO)
        {
            //no need to download update - refill data now
            //if downloading - wait for download
            [self FillData];
        }
    }


}


-(void)FinishDownload:(NSNotification *)notification
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self FillData];
    });

}

-(void)FillData
{
    //a lot of code - example of setting temperature
    NSString *str = [NSString stringWithFormat:@"%@ °C", act.temp_act];
    self.lblTemp.text = str;
    [self.lblTemp sizeToFit];


    if (self.completionHandler != nil)
    {
        self.completionHandler(NCUpdateResultNewData);
    }
}



- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler
{
    // Perform any setup necessary in order to update the view.

    // If an error is encountered, use NCUpdateResultFailed
    // If there no update required, use NCUpdateResultNoData
    // If there an update, use NCUpdateResultNewData

    //completionHandler(NCUpdateResultNewData);
    NSLog(@"=== widgetPerformUpdateWithCompletionHandler === ");

    self.completionHandler = completionHandler;

    [self updateData];


}

- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets
{
    return UIEdgeInsetsMake(0, 0, 5, 5);
}


- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize
{
    if (activeDisplayMode == NCWidgetDisplayModeExpanded)
    {
        self.preferredContentSize = CGSizeMake(320, 160);            
    }
    else if (activeDisplayMode == NCWidgetDisplayModeCompact)
    {
        self.preferredContentSize = maxSize;            
    }
}
+4
source share
3 answers
  • Life cycle view

    Do not duplicate work in viewDidLoadand viewWillAppear/viewDidAppear.
    The look that has been loaded will hit all three methods. Use viewDidLoadfor operations that must be performed exactly once for life UIViewController.
    Potential problem :
    Launching 3 calls, possibly conflicting, with [self updateData]backwards, possibly with competing completion NCUpdateResult 3 handlers .

  • Balance watchers

    , addObserver removeObserver. , view___Appear view___Disappear, fooobar.com/questions/272971/....
    :
    , .

  • NCUpdateResultNewData: NCUpdateResult widgetPerformUpdateWithCompletionHandler, , . , updateData , , FillData .

    if (nil != self.completionHandler) {
        self.completionHandler(NCUpdateResultNewData);
        self.completionHandler = nil; // One time use
    }
    

    widgetPerformUpdateWithCompletionHandler , fooobar.com/questions/822867/....

  • , iOS ; Interface Builder, , . . Autolayout //

+1

UILabel Interface Builder, , "" . , , . , " ", .

+1

-. .

, , - .

: - addObserver. ( , back-forward ..)

  • .

  • / VC

: / ViewWillAppear ViwDidAppear.

+1
source

Source: https://habr.com/ru/post/1660108/


All Articles