I was looking into iOS NUI Framework source code. I spotted the following line of codes but I couldn't figured out how it worked
- (void)override_didMoveToWindow
{
if (!self.isNUIApplied) {
[self applyNUI];
}
[self override_didMoveToWindow];
}
Just to be clear, they swizzled out the original implementation of DidMoveToWindow with this method in order to apply the class/style at run time. What confused me was that the function above never caused any infinite loop.
This may help: http://darkdust.net/writings/objective-c/method-swizzling
The swizzled method is actually exchanged with the original. So when the original method is called the swizzled method has already exchanged the implementation. And calling the "swizzled method" override_didMoveToWindow method will call the original function.
Comment the line
//[self override_didMoveToWindow];
works for me
It looks like not a loop because it appears the author assumes that [self applyNUI] always changes the state so that self.isNUIApplied becomes == YES
Related
I using the below code to set elements in an array and return the array, the returned array is then passed to another method for more changes before again being returned.
NSMutableArray *returnArray = [[NSMutableArray alloc]init];
//call checkTP1
returnArray = [self checkTP1STD:addingTime :startToTP1 :TP1Result :nowDate :sevenHour :totalrest :returnArray];
//call check TP2
returnArray = [self checkTP2STD:addingTime :startToTP2 :TP2Result :nowDate :sevenHour :totalrest :returnArray :tp2Rest];
It is currently working as expected, my question is will it always wait for the checkTP1STD to return before executing checkTP2STD?
I have split the code into multiple methods to enable it to be more readable as i will be adding some other logic to pass different variable values to the methods,just wanted to make sure my basic idea will work.
In general: yes
Your question is curious, you seem to be concerned that checkTP1STD will return before checkTP2STD is called, but not that the calls to alloc and init will return before the call to checkTP1STD.
Are you actually intending to do asynchronous work in checkTP1STD (E.g. Using GCD or system framework methods which state they are async). If so the answer is still yes, but the call may return before all the work scheduled by checkTP1STD is complete - the very nature of asynchronous programming.
HTH
In short, yes. Code is executed in sequential order unless there's explicit calls to new threads, which you're not doing by the code you've given.
I'm trying to track down an error that appears to be definitely a timing issue. I have an application that uses a Universal Framework. When the process is complete in the framework an NSNotification is sent back to the application. We have recently added a third party framework to our framework. Now, while the methods for the third party framework are being executed, as execution returns to our framework I receive the following error in the Console Output:
Assertion failed: (exclusive), function assert_locked, file ../dep/include/boost/boost_1_55_0/boost/thread/pthread/shared_mutex.hpp, line 51.
But I'm not sure that is the ultimate issue, because our framework continues to execute and the NSNotification is sent back to the application. Right after the Notification is sent and execution returns to the calling method (or the method call) in our framework I see a warning on the executing thread. Then, execution continues back to the original calling method and the warning goes away.
Here's the weird part. If I step through the code very slowly, it might just work. If I'm not slow enough I get the SIGSTOP and the code never returns to the UI. If I'm too fast, I get a SIGABRT.
I've been trying to find the exact issue using Instuments. This answer to a similar question backed up my suspicion that this is a timing issue. I think the boost assert_locked Assertion might have something to do with this.
My code is pretty boring but I know you want to see it, so here it is:
- (void)generateImageTemplates:(UIImage *)src
{
int result = 0;
cv::Mat image = *(cv::Mat*)[self cvMatFromUIImage:src];
user = IEngine_InitUser();
int userID=0;
result = IEngine_AddFingerprintRAW(user, UNKNOWN, image.data, image.size().width, image.size().height);
result = IEngine_RegisterUser(user, &userID);
[[NSNotificationCenter defaultCenter] postNotificationName:#"InnovatricsComplete" object:self];
}
If you're wondering what result is, it's an error code. So far these have all come back equal to 0. Meaning no errors. I'll work on handling these errors once I can get a successful return to the UI without crashing.
Control returns to the method call:
- (void)generateImageTemplates:(UIImage *)processedImage
{
[self.captureCommand generateImageTemplates:processedImage];
}
Control returns to the method call in the application View Controller:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
[self clearPressed:self.clearButton];
} else {
[self.cameraVC generateImageTemplates:self.processedImage];
}
}
Finally, the NSNotification callback code:
- (void)onInnovatricsComplete:(NSNotification *)note
{
[self.cameraVC willMoveToParentViewController:nil];
[self.cameraVC.view removeFromSuperview];
[self.cameraVC removeFromParentViewController];
}
I warned you it was pretty boring!
I'm completely stumped! Though I continue to surf the internet for clues, is there anybody out there who can help me resolve this issue?
Thank you.
Here are some screenshots (in reverse order):
look at NSUinteger’s answer in How to know where crash for postNotificationName:object:userInfo
the listener might be deallocated before it recieves the notification.
I have a method similar to the one below:
- (UIImage*)getImage {
return image;
}
Which returns an image loaded from a URL in another method. I only want the method above, however, to return once image is not longer 'nil' (i.e. once it has been loaded). What's the best way to approach this?
Thanks!
- (void)getImage:(void(^)(UIImage *image))callback{
//Load image asynchronously
if(callback){
callback(image);
}
}
I would like to use block to load an asynchronous image.
-(UIImage *)getImage {
//isLoaded is your own function
if ([self isImageLoaded:image) {
return image;
}
return nil;
}
You can't do that (or at least you shouldn't); that's not how asynchronous works. You ask for the download to start and that's all; the method is over. Later, when the download has completed, the downloading code calls back into your code on the main thread and says "I have an image for you, take it."
Here's the example code from my book showing a class designed (for iOS 7 only) for exactly this purpose. You call the method download:completionHandler: with the path of a URL and a block. Then you walk away and do nothing; you do not wait. You just go about your business, if any. When the download is finished, your block will be called and the image will be handed to you through that. Recognizing what image this is and dealing with it is up to you. The surrounding project shows an example (downloading images and popping them into a table view as they arrive).
I have a function call and some coding statements after the function. These statements should be called only after the function is executed completely How can it be achieved? Currently the statements are executed before the function is executed completely.
For example
NSInteger integerRestValue=[self buttonRestNameTag];
buttonRestNames.titleLabel.text=[[arrayGuessList objectAtIndex:integerRestValue]valueForKey:#"Name"];
Here the buttonRestNameTag function is called and before the execution is completed the buttonRestNames title label is set which cause it to crash.
How can this be resolved?
You may have initialized another Thread inside your function buttonRestNameTag.
Check that thing.
Or Try to use this function :
[self performSelectorOnMainThread:#selector(functionName) withObject:nil waitUntilDone:YES];
Hope this helps.
Edit for Kiron :
Make a variable in class and put returned value in that and access that variable.
This is helpful link to do this
iphone - performSelectorOnMainThread with return value
You can use GCD blocks
Try this.
I have gone down quite a few paths and wondered if someone could spot this right off:
I want to display a button when a text field is selected (hence the keyboard is loaded).
From what i've read NSNotificationCenter is one way to do this:
So in viewWillAppear I placed the following
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidAppear:) name:UIKeyboardWillShowNotification object:self.view.window];
for the destination (selector) i have:
-(void)keyboardDidAppear{
_button2.hidden = FALSE;
}
the program aborts to a crash dump (sure seems like there should be a better debugging path than this)
if I comment out the only line in keyboardDidAppear
// _button2.hidden = FALSE;
the program still aborts
so evidently the NSNotificationCenter is firing and is not tolerated for some reason
Any simplified help would be appreciated.
Seriously, the crash dump is all you get when theres an execution error? Is it all you need?
The problem is simple. You register the method keyboardDidAppear: but you supply a method named keyboardDidAppear. These are not the same (notice the colon).
Change one or the other (but not both).
Another solution would be to implement the UITextFieldDelegate method textFieldDidBeginEditing: and show the button when this is called.
You are trying to send a message to keyboardDidAppear: but you implemented keyboardDidAppear. The first method is a method taking a parameter, but you supplied a method taking no parameters, which of course is different.
Change either your method definition to
-(void)keyboardDidAppear:(NSNotification *)n {
_button2.hidden = NO; //Please stick to Obj-C semantics and use NO, as opposed to FALSE.
}
or the call to
#selector(keyboardDidAppear)