I'm having some really weird behavior in my UIViewController. I'm connecting two devices via the Multipeer Framework and once a iPhone starts sending data I want the other iPhone to display a UIViewController which handles the received data. So once iPhone A starts sending, iPhone B posts a NSNotification. The notification is arriving pretty fast and the code below is being executed:
- (void)presentPeerView:(NSNotification *)notif
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ClientViewController *clientView = (ClientViewController *)[storyboard instantiateViewControllerWithIdentifier:#"peerView"];
[clientView setImage:_image];
clientView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.window.rootViewController presentViewController:clientView animated:YES completion:nil];
}
Now here comes the weird part. First of all, the ClientViewController gets displayed. But there is no animation, it just appears out of nowhere. My viewDidLoad gets called and runs following code:
- (void)viewDidLoad
{
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[self blurImage:_image];
[self.imageView setImage:_image];
}
Now, I see the blurred image, which is fine. But the (non blurred) image dosen't displays in the UIImageView. I mean the image get's passed properly, otherwise I couldn't see the blurred Background.
I also have a few Interface elements from the Storyboard:
So the UIViewController loads with the blurred background Image:
As you can see, none of the Storyboard UI Elements are loaded, except for the UISwitch (which looks quite strange, too).
After a couple of seconds (it depends, sometimes up to 30, sometimes only 3 seconds) the whole UI loads up:
So, maybe someone of you had the same issue before, I really don't understand why the UI does take so much time to "load". The CPU percentage is somewhere around 3%.
I also added a NSLog() to the this completion block:
[self.window.rootViewController presentViewController:clientView animated:YES completion:^{
NSLog(#"done.");
}];
which is called instantly. The viewDidLoad seems to work fine, there are no lines it might get stuck, if I do a NSLog at the end of viewDidLoad it gets called instantly as well, but the view (as you can see in the images below) isn't loaded (completely).
From the MCSession class reference:
Important: Delegate calls occur on a private operation queue. If your app needs to perform an action on a particular run loop or operation queue, its delegate method should explicitly dispatch or schedule that work.
You're seeing this delay because you're doing UIView work on the wrong thread.
There's a number of ways to fix this, for example:
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID {
dispatch_async(dispatch_get_main_queue(), ^{
// post your notification or display your view
});
}
Related
I seem to be having the exact opposite problem that pops up my question searches. I have a splitViewController that seems to be having a slow time updating its master view, after I call setNeedsDisplay. Eventually the update request gets drawn, but it is randomly between 5 to 150 seconds after the change should occur.
If I immediately rotate the iPad, the view changes are immediately reflected.
The layout is:
SVC - Detail VC
\
+-Navigation VC
\
MasterVC
+--UILabel (hidden/unhidden)
|
+--UIButton
All I want to do is hide/unhide a label in the MastVC when an action takes place in the MasterVC. On viewDidLoad, the label is hidden. When a button is pushed on the MasterVC, the label is unhidden and then things just don't go.
I have set everything under the sun to "setNeedsDisplay", but nothing makes it happen as fast as it should. If I even pushed all the setNeedsDisplay methods into dispatch_async(dispatch_get_main_queue(), ^{ ... }; there are no immediate results (not that I'm on a different thread, but it seemed like a good thing to try after reading similar questions).
I have made these calls from the SplitVC, the NavVC, the Master, each subVC, each subView, I even set up a Notification Center call from the Master to the SVC to have the SVC do the update specifically after the label was flagged as unhidden.
This all started to seem exceedingly off track, just to show/hide a simple label. Especially when all I have to do for the label show up properly is to just rotate the iPad.
As I said, the label eventually shows in the right spot, so it isn't off frame or opaque = 0 or something like that.
When I push the connect button, I make a call to Bluetooth Central Manager. Once the BCM connects to the device, I get a NC key/value that confirms connection. This triggers the label to be unhid.
-(void) receiveBCMNotification: (NSNotification *) notification {
NSDictionary *userInfo = notification.userInfo;
NSLog(#"got a BCM notice: %#",userInfo);
if ([[userInfo allKeys] containsObject:ddkBltCentralManagerStatusKey]) {
if ([[userInfo valueForKey:ddkBltCentralManagerStatusKey] isEqualToString:ddvBltCentralMangerScanningStarted]) {
[self.refreshAvailablePatches beginRefreshing];
}
if ([[userInfo valueForKey:ddkBltCentralManagerStatusKey] isEqualToString:ddvBltCentralMangerScanningEnded]) {
[self.refreshAvailablePatches endRefreshing];
[self.availablePatchesTableView reloadData];
}
if ([[userInfo valueForKey:ddkBltCentralManagerStatusKey] isEqualToString:ddvBltCentralMangerDeviceConnected]) {
self.connectedToPatchVisual.hidden = NO;
[self.view setNeedsDisplay];
NSDictionary *newInfo = [NSDictionary dictionaryWithObject:ddvMasterSideViewNeedsDisplay forKey:ddkMasterSideViewNeedsDisplay];
[[NSNotificationCenter defaultCenter] postNotificationName: ncMasterSideNotifications object:nil userInfo:newInfo];
}
}
}
I have a strange issue using presentViewController as part of a library.
The code that is using the library calls this method. It takes up to 12 seconds from calling presentViewController to running the completion block. But not all the time normally it's almost instantaneous.
However if I touch any where on the screen while it is "lagging" it will fire instantly.
-(void) advancedMenuWithPresentingViewController
:(UIViewController *)presentingViewController
animated:(BOOL)animated
onClose:(void (^)(void))onClose
onPrint:(void (^)(NSString *, NSString *))onPrint{
AdvancedMenuViewController *amc = [[AdvancedMenuViewController alloc] init];
AdvancedMenuViewController * __weak weakAmc = amc;
[amc setOnClose:^(void)
{
[weakAmc dismissViewControllerAnimated:animated completion:^{
onClose();
}];
}];
[amc setOnPrint:onPrint];
//Time from here
[presentingViewController presentViewController:amc
animated:animated
completion:^{
//To here
}];
}
viewDidLoad and viewWillAppear are called without any lag and then there is a long delay (unless you touch the screen) until viewDidAppear is called.
There is nothing inside any of these methods that would slow it down. As it normally works fine.
If any one could shed any light on this issue I would be most grateful, the most confusing part is that if you interact with the screen after firing advancedMenuWithPresentingViewController it will occur instantly.
Replacing
[presentingViewController presentViewController:amc
animated:animated
completion:^{
//To here
}];
with
dispatch_async(dispatch_get_main_queue(), ^{
[presentingViewController presentViewController:amc
animated:animated
completion:^{
//To here
}];
});
Resolved the issue.
Thanks to rmaddys suggestion.
Run the app in Instruments with the Time Profiler.
That should at least tell you if you have some slow drawing code or if something in UIKit is slowing it down.
I loaded a UIView from a UIViewController. This UIView contains a (big) UICollectionView.
The transition from the first UIView to the second UIView is very slow: It seems that when the rendering of all collection's cells is done the second view can show up.
In the second UIView, I tried.
- (void)viewDidAppear:(BOOL)animated
{
[activityView stopAnimating];
NSLog(#"did appear %#",[NSDate date]);
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[activityView startAnimating];
NSLog(#"will appear %#",[NSDate date]);
}
In the NSLog, there is no time difference between the two events, and in fact the second UIView shows up in about 1 second after the event viewDidAppear.
At this point, I would start a UIActivityIndicator, as in the code. But the indicator is never shown.
Any hint?
Your problem here is that you're probably blocking the main thread by maybe doing some disk IO or network activity or heavy computations, and that is why you're experiencing this delay.
I'd recommend that you do all this on a secondary thread while showing a UIActivityIndicator. On the completion you can then hide the activity indicator and show the collection view.
EDIT:
N.B. There is probably a better way to go, but i'm not very familiar with collection views.
A really easy fix would be to keep a BOOL ivar in the view controller where you load the collection view. Call it shouldLoadData and set it to NO in your viewDidLoad method. Then all you need to do is to return 0 to your UICollectionViewDelegate methods numberOfSectionsInCollectionView: and collectionView:numberOfItemsInSection:.
Finally in your viewDidAppear method, you set shouldLoadData to YES and call reloadData on your collectionView. The tricky part at this point is to figure out a way to tell when the collection view finished reloading its data so that you can stop the activity indicator.
I found out that it is not even that tricky, reloadData just queues up on the main thread, so you can just queue another task on the main thread after you make the call to reloadData. Just do:
[self.collectionView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
[self.activity stopAnimating];
});
And you'll get the desired behaviour. You should be aware, however, that this would still block the main thread.
E.g. if you have a back button, it could not be pressed until the data is fully loaded (it could actually be pressed, but it would not have any visible effect until then).
This is my first question so excuse me for being a newbie.
I am working with a CollectionView that shows images downloaded from the internet. The problem appears when I try to do it asynchronously.
#interface myViewController
{
NSArray *ArrayOfSections
}
#end
-(void)viewDidLoad{
[self performSelectorInBackground:#selector(refreshImages) withObject:nil];
}
-(void)refreshImages{
... //Get information from the net
NSArray internetInfo = ...;
[self performSelectorOnMainThread:#selector(refreshCollectionView:) withObject:internetInfo waitUntilDone:NO];
}
-(void)refreshCollectionView:(NSArray tempArray){
ArrayOfSections = tempArray;
}
This code is not working. It shows an empty CollectionView, although I have double checked that the information stored on ArrayOfSections is correct.
Moreover, if I do it synchronously (I change only viewDidLoad).
-(void)viewDidLoad{
[self refreshImage];
}
Everything works fine. I am going bananas. Please help
I think it's because you're not telling the collection view to reload. Your refresh method updates the model but not the view.
If you're fetching the data on a background thread, the main thread can continue it's lifecycle, which involves querying the collection view datasource and delegate methods then updating the view, but it will be doing this too soon in your case, as the model isn't ready. That's why you need to tell it to do that again, when the model is ready, at the end of your data fetch. Since you block the thread when doing it synchronously, it won't reach the collection view methods until the model is ready, which is why it works that way.
I'm making a split-view based iPad application(Portrait mode only), and I want to know how to recover initial state after viewDidUnload is called.
When split-view application started for the first time,
-splitViewController:willHideViewController:withBarButtonItem:forPopoverController:
is called automatically (right after -viewDidLoad).
I prepares UIBarButtonItems in the method.
If I open modal dialog or something with UIWebViewController (it consumes a lot of memory), application receives memory warning, viewDidUnload(s) are called.
When I close the modal dialog, -viewDidLoad is called automatically, but this time
-splitViewController:willHideViewController:withBarButtonItem:forPopoverController: is not called.
I prepares UIBarButtonItems in
-splitViewController:willHideViewController:withBarButtonItem:forPopoverController:
but it is not called, so buttons are dismissed.
In that case, should I call the method manually?
I found similar posting here.
https://github.com/grgcombs/IntelligentSplitViewController/issues/6
Thanks.
I don't know it is OK to answer to my own question, but maybe I found an answer for this. http://osdir.com/ml/cocoa-dev/2011-02/msg00430.html
It says that we should preserve BarButtonItems in viewDidUnload, and load it in viewDidLoad.
It seems working fine.
- (void)viewDidUnload {
[super viewDidUnload];
self.toolbarItems = self.toolbar.items; // property with retain policy
}
- (void)viewDidLoad {
[super viewDidLoad];
if (self.toolbarItems) {
self.toolbar.items = self.toolbarItems;
self.toolbarItems = nil;
}
}