Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prepare UI update before app enter foreground

I know update UI when App in the background is not recommended by Apple, especially for OpenGL.

However, I just realized iMessenger and Facebook's messenger looks like able to do that. Enter a message thread with your friend and then go background, then receive a new message while the app is still in the background, then bring this app to foreground (click the push notification or App icon), you will find the new message bubble is already there with the app expansion animation from the icon.

For my understanding, this can only happen because the new message bubble is already drawn in the background mode. Then while app entering foreground, it can appear in the animation.

However from my test result, in iOS8 and iOS9, all the background UI update will be postponed after the app did become active. And furthermore, iOS will add an implicit animation transaction for that UI update.

I listed my test code as below, you will see the new cell be added to the table with an obvious animation transaction when the app going foreground, totally unlike the iMessenger did. The tableView:numberOfRowsInSection: will only be delay executed when app enters foreground.

And not only for the tableview cell update, even adding a subview in the background will also trigger similar delayed transaction for entering foreground.

Maybe I'm in a totally wrong direction about this. Could anyone help me to understand how iMessenger and FB's messenger able to achieve this effect? Thanks in advance!

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *dataTable;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
  [self.view addSubview:self.tableView];
  self.tableView.delegate = self;
  self.tableView.dataSource = self;
  [self.tableView registerClass:[UITableViewCell class]
         forCellReuseIdentifier:@"kCellId"];

  self.dataTable = [[NSMutableArray alloc] initWithArray:@[@"1", @"2", @"3"]];

  __weak __typeof(self) weakSelf = self;
  [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification
                                                    object:nil
                                                     queue:[NSOperationQueue mainQueue]
                                                usingBlock:^(NSNotification * _Nonnull note) {
                                                  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),
                                                                 dispatch_get_main_queue(), ^{
                                                    [weakSelf.dataTable addObject:[@(weakSelf.dataTable.count + 1) stringValue]];
                                                    [weakSelf.tableView reloadData];
                                                  });
                                                }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.dataTable.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kCellId"];
  cell.textLabel.text = self.dataTable[indexPath.row];
  return cell;
}

@end
like image 658
Pei Avatar asked Dec 16 '15 03:12

Pei


People also ask

What is foreground in UI?

Overview. Use foreground transitions to prepare your app's UI to appear onscreen. An app's transition to the foreground is usually in response to a user action. For example, when the user taps the app's icon, the system launches the app and brings it to the foreground.

What does it mean to have an app open in the foreground?

In the foreground: The app can use your location only when the app is open on your screen or when you ask the app to do something. In the background: The app can use location info at any time, even if you aren't using it.

How do I keep iOS apps running in foreground?

To keep apps running in the background on iPhone, you can turn on the background app refresh feature. Go to Settings, General, Background App Refresh and turn it on. In the following apps, toggle on the ones you want to keep running.


1 Answers

I can recommend you to look into APN payload documentation for launch-mage.

launch-image - The filename of an image file in the app bundle; it may include the extension or omit it. The image is used as the launch image when users tap the action button or move the action slider. If this property is not specified, the system either uses the previous snapshot,uses the image identified by the UILaunchImageFile key in the app’s Info.plist file, or falls back to Default.png. This property was added in iOS 4.0.

like image 163
Ramis Avatar answered Oct 18 '22 09:10

Ramis