How to hold for a moment in program? - iphone

I am fetching data from facebook and during the time of fetching the data, it is taking few milliseconds or a second. I want to be on hold untill fetched data perfectly.
How to write code for this?

- (void)viewDidLoad
{
[self performSelectorInBackground:#selector(startFatching) withObject:nil];
[super viewDidLoad];
}
- (void) startFatching
{
[self performSelectorOnMainThread:#selector(startIndicator) withObject:nil waitUntilDone:NO];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// your code
[self performSelectorOnMainThread:#selector(stopIndicator) withObject:nil waitUntilDone:NO];
[pool release];
}
- (void) startIndicator
{
av.hidesWhenStopped = YES;
[av startAnimating];
}
- (void) stoprIndicator
{
[av stopAnimating];
}
hope this will give you some idea

Related

Can we thread the web-service and insert in in custom table cell?

The thing is i have to make Custom cell of UITable, i have to call large webservice with many data which will take longer tym to load it once. So i want to apply threading in it. now my que is. can we at run -time insert value one by one in custom cell , as user will see data comming one by one at run time..?? if yes how we can do that.
yes you can ... try this
- (void)viewDidLoad
{
myApp = (myAppDelegate *) [[UIApplication sharedApplication] delegate];
[self performSelectorInBackground:#selector(startParsing) withObject:nil];
[super viewDidLoad];
}
- (void) startParsing
{
[self performSelectorOnMainThread:#selector(startIndicator) withObject:nil waitUntilDone:NO];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [[NSURL alloc] initWithString:#"http://abc.com"];
NSData *data = [NSData dataWithContentsOfURL:url];
// 2 -- parsing
parser = [[VolMyParser alloc] init];
[parser parseXML:data];
[data release];
//[parser print];
[self performSelectorOnMainThread:#selector(updateTable) withObject:nil waitUntilDone:NO];
[pool release];
}
- (void) startIndicator
{
av.hidesWhenStopped = YES;
[av startAnimating];
}
- (void) updateTable
{
[av stopAnimating];
[myTable reloadData];
}
Hope its gives you an Idea...

RSS on a different thread doesnt work but works fine when on the main thread

I'm trying to get an rss parser on a different thread in my iphone app, but when I do this I only get the spinning indicator (i.e., nothing). But if I comment out the call [NSThread....] in viewDidAppear, and uncomment the line [self loadData], everything works (but then its not on a different thread). Am I missing something? Thanks for any insight you can provide here!!
Here is the code.
- (void)viewDidAppear:(BOOL)animated {
[NSThread detachNewThreadSelector:#selector(loadData) toTarget:self withObject:nil];
//[self loadData];
[super viewDidAppear:animated];
}
- (void)loadData {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (items == nil) {
[activityIndicator startAnimating];
Parser *rssParser = [[Parser alloc] init];
[rssParser parseRssFeed:#"http://www.mywebsite.com/xml" withDelegate:self];
[rssParser release];
} else {
[self.tableView reloadData];
}
[pool release];
}
All UI changes should be made on the main thread:
- (void)viewDidAppear:(BOOL)animated {
if (items == nil)
{
[activityIndicator startAnimating];
[NSThread detachNewThreadSelector:#selector(loadData) toTarget:self withObject:nil];
}
else
{
[self.tableView reloadData];
}
[super viewDidAppear:animated];
}
- (void)loadData {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Parser *rssParser = [[Parser alloc] init];
[rssParser parseRssFeed:#"http://www.mywebsite.com/xml" withDelegate:self];
[rssParser release];
[pool release];
}
Check if items is null, if it is, start animating the indicator and then start the new thread.

Image view not updating on NSThread

I'm trying to make a test app which measures the performance for threads. However, I'm trying to set an image to imageview on a thread and it's not updating. In fact, the methods setImage1 and setImage2 aren't even getting called. Any ideas?
Look at the code for details
#import "UntitledAppDelegate.h"
#import <QuartzCore/QuartzCore.h>
#implementation UntitledAppDelegate
#synthesize window, imageView1, imageView2, label1, label2, label0;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[window makeKeyAndVisible];
return YES;
}
-(IBAction) startSeparate:(id) sender
{
imageView1.image = imageView2.image = nil;
double prev;
start = prev = CACurrentMediaTime();
[self setImage1];
label1.text = [NSString stringWithFormat:#"%.10g", CACurrentMediaTime() - prev];
prev = CACurrentMediaTime();
[self setImage2];
label2.text = [NSString stringWithFormat:#"%.10g", CACurrentMediaTime() - prev];
}
-(IBAction) startThreaded:(id) sender
{
imageView1.image = imageView2.image = nil;
double prev;
NSThread *t1 = [[NSThread alloc] init];
[t1 start];
NSThread *t2 = [[NSThread alloc] init];
[t2 start];
start = prev = CACurrentMediaTime();
//This doesn't work
//[self performSelector:#selector(setImage1) onThread:t1 withObject:nil waitUntilDone:NO];
//But this does
[self performSelectorInBackground:#selector(setImage1) withObject:nil];
label1.text = [NSString stringWithFormat:#"%.10g", CACurrentMediaTime() - prev];
prev = CACurrentMediaTime();
//This doesn't work
//[self performSelector:#selector(setImage2) onThread:t2 withObject:nil waitUntilDone:NO];
//But this does
[self performSelectorInBackground:#selector(setImage2) withObject:nil];
label2.text = [NSString stringWithFormat:#"%.10g", CACurrentMediaTime() - prev];
}
-(void) setImage1
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *image1 = [UIImage imageNamed:#"bird.png"];
imageView1.image = image1;
[self pictureDone];
[pool drain];
}
-(void) setImage2
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *image2 = [UIImage imageNamed:#"bird.png"];
imageView2.image = image2;
[self pictureDone];
[pool drain];
}
-(void) pictureDone
{
done++;
NSLog(#"%i", done);
if (done == 2) {
label0.text = [NSString stringWithFormat:#"%.10g", CACurrentMediaTime() - start];
done = 0;
}
}
#end
Never change anything in a currently displayed UIView from a background thread. There are a million different synchronization problems that you can encounter. Download the image in the background thread, and then call another method on the main thread (from the background thread) to actually update the UIImageView
performSelectorOnMainThread:...
As for the problem with the method not running, try this instead:
[self performSelectorInBackground:#selector(setImage2:) withObject:nil];
(adding a colon after setImage2).
Also, I don't think those two NSThread objects (t1 and t2) are necessary, and they are also probably creating a memory leak.
Technically, NSThread itself do not create run loop automatically, then the performSelector:onThread won't work if you just create thread that way.

Threading / UIActivityIndicatorView with In App Purchase

Let me first say, my In App Purchase works.
I have been struggling with the Activity Indicator / Threading for more than a week now. I am having real problems getting my spinner (UIActivityIndicatorView) to play nice within my InAppPurchase.m
I am using the same threading code in many other places and it works fine.
Is there something about how the IAP process works that causes problems with basic threading?
Right now, the spinner spins between the time you tap the buyButton and the first alert appears ("Do you want to buy? ..."), but no spinner after that.
Here is the .m File:
// InAppPurchaseManager.m
#import "InAppPurchaseManager.h"
#import "GANTracker.h"
#implementation InAppPurchaseManager
#synthesize productID;
#synthesize productsRequest;
#synthesize closeButton;
#synthesize buyButton;
#synthesize testLabel;
#synthesize pView;
#synthesize spinner;
#synthesize spinnerLabel;
- (void)dealloc {
[productID release];
//[productsRequest release];
[closeButton release];
[buyButton release];
[testLabel release];
[pView release];
[spinner release];
[spinnerLabel release];
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSError *error;
if (![[GANTracker sharedTracker] trackPageview:#"/in_app_purchase" withError:&error]) {
//NSLog(#"No GAN Tracking");
}
UIColor *backgroundColor = [UIColor colorWithRed:.6745 green:.1333 blue:.1333 alpha:1];
pView.backgroundColor = backgroundColor;
[closeButton release];
closeButton = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStyleBordered target:self action:#selector(closeButtonAction:)];
self.navigationItem.leftBarButtonItem = closeButton;
// create the "Loading..." label
[spinnerLabel release];
//CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
spinnerLabel = [[UILabel alloc] initWithFrame:CGRectMake(35, 145, 250, 35)];
[spinnerLabel setText:#"Connecting to App Store... "];
[spinnerLabel setTextColor:[UIColor whiteColor]];
[spinnerLabel setBackgroundColor:[UIColor blackColor]];
[spinnerLabel setTextAlignment:UITextAlignmentRight];
[self.view addSubview:spinnerLabel];
spinnerLabel.hidden = YES;
// create the spinner
[spinner release];
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[spinner setCenter:CGPointMake(55,162)];
[self.view addSubview:spinner];
spinner.backgroundColor = [UIColor blackColor];
spinner.hidesWhenStopped = YES;
[spinner stopAnimating];
self.navigationItem.title = #"Credits";
//[self spinTheSpinner];
//[self loadStore];
[NSThread detachNewThreadSelector:#selector(loadStore) toTarget:self withObject:nil];
}
-(void)viewDidAppear:(BOOL)animated {
[self doneSpinning];
[self updateButtonStatus:#"ON"];
}
-(void)spinTheSpinner {
NSLog(#"In App Purchase.m == SpinTheSpiner");
//NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[spinner startAnimating];
spinnerLabel.hidden=NO;
//[self performSelectorOnMainThread:#selector(doneSpinning) withObject:nil waitUntilDone:NO];
//[pool release];
}
-(void)doneSpinning {
NSLog(#"In App Purchase.m == DoneSpinning");
spinnerLabel.hidden = YES;
[spinner stopAnimating];
}
-(void)closeButtonAction:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
-(void)buyButtonAction:(id)sender {
if([self canMakePurchases]) {
[self updateButtonStatus:#"OFF"];
[self spinTheSpinner];
//[self performSelectorOnMainThread:#selector(requestInAppPurchaseData) withObject:nil waitUntilDone:NO];
[NSThread detachNewThreadSelector:#selector(requestInAppPurchaseData) toTarget:self withObject:nil];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithString:#"Your account settings do not allow for In App Purchases."] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}
-(void)updateButtonStatus:(NSString *)status {
if ([status isEqual:#"OFF"]) {
closeButton.enabled = NO;
buyButton.enabled = NO;
buyButton.titleLabel.textColor = [UIColor grayColor];
} else {
closeButton.enabled = YES;
buyButton.enabled = YES;
buyButton.titleLabel.textColor = [UIColor blueColor];
}
}
#pragma mark -
#pragma mark SKProductsRequestDelegate methods
//
// call this method once on startup
//
- (void)loadStore
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(#"Load Store");
//[NSThread detachNewThreadSelector:#selector(spinTheSpinner) toTarget:self withObject:nil];
// restarts any purchases if they were interrupted last time the app was open
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[self doneSpinning];
[pool release];
}
- (void)requestInAppPurchaseData
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(#"Request In App Purchase Data");
//[NSThread detachNewThreadSelector:#selector(spinTheSpinner) toTarget:self withObject:nil];
NSSet *productIdentifiers = [NSSet setWithObject:kInAppPurchaseCreditProductId];
productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
productsRequest.delegate = self;
[productsRequest start];
//[self doneSpinning];
[pool release];
// we will release the request object in the delegate callback
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSLog(#"did Receive Response");
//[NSThread detachNewThreadSelector:#selector(spinTheSpinner) toTarget:self withObject:nil];
NSArray *products = response.products;
productID = [products count] == 1 ? [[products objectAtIndex:0] retain] : nil;
if (productID)
{
/*
NSLog(#"Product title: %#" , productID.localizedTitle);
NSLog(#"Product description: %#" , productID.localizedDescription);
NSLog(#"Product price: %#" , productID.price);
NSLog(#"Product id: %#" , productID.productIdentifier);
*/
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *currentCredits = ([standardUserDefaults objectForKey:#"currentCredits"]) ? [standardUserDefaults objectForKey:#"currentCredits"] : #"0";
testLabel.text = [NSString stringWithFormat:#"%#", currentCredits];
}
for (NSString *invalidProductId in response.invalidProductIdentifiers)
{
//NSLog(#"Invalid product id: %#" , invalidProductId);
testLabel.text = #"Try Again Later.";
}
// finally release the reqest we alloc/init’ed in requestProUpgradeProductData
[productsRequest release];
[[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];
//[self performSelectorOnMainThread:#selector(purchaseCredit) withObject:nil waitUntilDone:NO];
[self purchaseCredit];
}
//
// call this before making a purchase
//
- (BOOL)canMakePurchases
{
NSLog(#"Can Make Payments");
//[NSThread detachNewThreadSelector:#selector(spinTheSpinner) toTarget:self withObject:nil];
return [SKPaymentQueue canMakePayments];
}
//
// kick off the upgrade transaction
//
- (void)purchaseCredit
{
// REMOVED FOR PRIVACY
}
#pragma -
#pragma Purchase helpers
//
// saves a record of the transaction by storing the receipt to disk
//
- (void)recordTransaction:(SKPaymentTransaction *)transaction
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//[NSThread detachNewThreadSelector:#selector(spinTheSpinner) toTarget:self withObject:nil];
if ([transaction.payment.productIdentifier isEqualToString:kInAppPurchaseCreditProductId])
{
// save the transaction receipt to disk
[[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:#"InAppPurchaseTransactionReceipt" ];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[pool release];
}
//
// enable pro features
//
- (void)provideContent:(NSString *)productId
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//[NSThread detachNewThreadSelector:#selector(spinTheSpinner) toTarget:self withObject:nil];
if ([productId isEqualToString:kInAppPurchaseCreditProductId])
{
// Increment currentCredits
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *currentCredits = [standardUserDefaults objectForKey:#"currentCredits"];
int newCreditCount = [currentCredits intValue] + 1;
[standardUserDefaults setObject:[NSString stringWithFormat:#"%d", newCreditCount] forKey:#"currentCredits"];
testLabel.text = [NSString stringWithFormat:#"%d", newCreditCount];
}
[pool release];
}
//
// removes the transaction from the queue and posts a notification with the transaction result
//
- (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:(BOOL)wasSuccessful
{
//[NSThread detachNewThreadSelector:#selector(spinTheSpinner) toTarget:self withObject:nil];
// remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, #"transaction" , nil];
if (wasSuccessful)
{
// send out a notification that we’ve finished the transaction
[[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionSucceededNotification object:self userInfo:userInfo];
}
else
{
// send out a notification for the failed transaction
[[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionFailedNotification object:self userInfo:userInfo];
}
[self updateButtonStatus:#"ON"];
}
//
// called when the transaction was successful
//
- (void)completeTransaction:(SKPaymentTransaction *)transaction
{
//[NSThread detachNewThreadSelector:#selector(spinTheSpinner) toTarget:self withObject:nil];
[self updateButtonStatus:#"OFF"];
[self spinTheSpinner];
[NSThread detachNewThreadSelector:#selector(recordTransaction:) toTarget:self withObject:transaction];
[NSThread detachNewThreadSelector:#selector(provideContent:) toTarget:self withObject:transaction.payment.productIdentifier];
//[self recordTransaction:transaction];
//[self provideContent:transaction.payment.productIdentifier];
[NSThread detachNewThreadSelector:#selector(threadFinishTransaction:) toTarget:self withObject:transaction];
//[self finishTransaction:transaction wasSuccessful:YES];
NSError *error;
if (![[GANTracker sharedTracker] trackPageview:#"/in_app_purchase_done" withError:&error]) {
//NSLog(#"No GAN Tracking");
}
[self doneSpinning];
}
-(void)threadFinishTransaction:(SKPaymentTransaction *)transaction {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self finishTransaction:transaction wasSuccessful:YES];
[pool release];
}
//
// called when a transaction has been restored and and successfully completed
//
- (void)restoreTransaction:(SKPaymentTransaction *)transaction
{
//[NSThread detachNewThreadSelector:#selector(spinTheSpinner) toTarget:self withObject:nil];
[self recordTransaction:transaction.originalTransaction];
[self provideContent:transaction.originalTransaction.payment.productIdentifier];
[self finishTransaction:transaction wasSuccessful:YES];
}
//
// called when a transaction has failed
//
- (void)failedTransaction:(SKPaymentTransaction *)transaction
{
//[NSThread detachNewThreadSelector:#selector(spinTheSpinner) toTarget:self withObject:nil];
if (transaction.error.code != SKErrorPaymentCancelled)
{
// error!
NSError *error;
if (![[GANTracker sharedTracker] trackPageview:#"/in_app_purchase_error" withError:&error]) {
//NSLog(#"No GAN Tracking");
}
[self finishTransaction:transaction wasSuccessful:NO];
}
else
{
// this is fine, the user just cancelled, so don’t notify
NSError *error;
if (![[GANTracker sharedTracker] trackPageview:#"/in_app_purchase_cancel" withError:&error]) {
//NSLog(#"No GAN Tracking");
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
[self updateButtonStatus:#"ON"];
}
#pragma mark -
#pragma mark SKPaymentTransactionObserver methods
//
// called when the transaction status is updated
//
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
//[NSThread detachNewThreadSelector:#selector(spinTheSpinner) toTarget:self withObject:nil];
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
break;
default:
break;
}
}
}
#end
Chris, 2 things:-
Firstly, why have you opted for a multi-threaded approach?
Nothing here requires you spawn a new thread. The StoreKit api is asynchronous, as you know, you are afterall using the callbacks and delegates. This is specifically so it won't block the main thread and so you don't have to spawn a new thread. It almost certainly does it's work on a background thread - but you don't need to know that, it is handled for you. Infact, not only does this code not require a background thread, you are almost certainly experiencing a substantial performance cost spawning new threads to do so little work. ie. it will (probably) take longer for the thread to start up than it will to do the work you have scheduled on it.
So, if your motivation was performance, you are going to be disapointed.
Secondly, your threading code, or lack of it, is a mess. On the plus side, just to reiterate, none of it is needed, so no big problem.
You say that you are
using the same threading code in many
other places and it works fine
You have been unlucky. This has given you the impression that this should work when infact it is completely unsafe. Threading is really tough, and if you want to do it you could do worse than reading some the related apple docs
Threading
Concurrency
I'm reluctant to just spew stuff straight from these guides, translated thru my foggy brain, and try to pass it of as my own advice, but to try to motivate you to read the guides i've adding some comments to a couple of lines of your code:-
// you start a new background thread to call -loadStore
[NSThread detachNewThreadSelector:#selector(loadStore) toTarget:self withObject:nil];
// you initialize SKPaymentQueue singleton on the background thread - is this allowed? i dont know. I can't see it documented.
// then you add transactionObserver observer on the background thread - which thread do you want to receive notifications on? 1)Main thread, 2)this (background) thread, 3)unsure. If it's not the background thread this probably isnt a good idea
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
// Update the GUI from the background thread. No joking here - you absolutely can never do this. It's well documented.
[self doneSpinning];
// end of method, background thread exits or not? You tell me. hope we get lucky with those notifications
[pool release];
So, i'd like to add that i am definitely no expert on how In App Purchase works but i would bet there is nothing special about it. Your activity spinner will probably be fine if you get rid of the background thread or re-implement it in a thread safe manner (which in my opinion doesn't look worth the trouble here).
I only skimmed your code, but I saw at least two places where it looked like you are attempting to update UI (stopping a spinner, updating label text) from a method which is called from a thread that is not the primary (main) thread. This is not allowed - all UI must be updated from the main thread.
If you need to update UI from a background thread you need to marshall the call to the main thread, perhaps using peformSelectorOnMainThread:withObject:
So, for example, it looks like loadStore is called from a non-main thread, and it calls doneSpinning, which updates UI. I'd make the following change:
- (void)loadStore
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(#"Load Store");
// restarts any purchases if they were interrupted last time the app was open
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
// instead of calling doneSpinning directly, ensure it runs on the main thread
[self performSelectorOnMainThread: #selector( doneSpinning) withObject: nil];
[pool release];
}

Painting a UIView from a background NSThread?

I am developing GL paint application.
To paint any object, I am using UIView that implemented.
Starting paint method :
- (void)viewDidLoad {
....
[NSThread detachNewThreadSelector:#selector(paintingObjects)
toTarget:self
withObject:nil];
}
- (void)paintingObjects {
while(1) {
[NSThread sleepForTimeInterval:2];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
ImplementedView *tmp = [self view];
[tmp draw];
[pool drain];
}
}
But it is not working (doesn't paint object).
What is wrong here ?
Please help me guys.
Thanks in advance.
All user-interface classes are not thread-safe and must be called from the main thread:
- (void)paintingObjects {
while(1) {
[NSThread sleepForTimeInterval:2];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
ImplementedView *tmp = [self view];
[tmp performSelectorOnMainThread:#selector(draw) withObject:nil waitUntilDone:YES];
[pool drain];
}
}
In this case, you would likely be better off using NSTimer.