UA-17470720-3

Jump to content


Photo
- - - - -

how do I show custom activity indicator while data is fetching from the service?

models sections actions view-controllers web-services

  • Please log in to reply
6 replies to this topic

#1 Balachandhar

Balachandhar

    Experienced Member

  • STV 3.0 Pro
  • PipPipPipPip
  • 87 posts
Reputation: 21
Excellent

Posted 27 May 2013 - 03:20 AM

Hi Tarek,

 

I want to show an activity indicator while the data is fetching from the server. 

 

is there any methods available to get the event before going to fetch the data and after the successfull fetch action?

 

thanks,

bala

 



#2 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 575 posts
  • LocationThe Grid
Reputation: 149
Popular

Posted 27 May 2013 - 08:45 PM

Greetings Program!

 

I still haven't been able to disable the default activity indicator that shows up in the cell but this is how I show the MBProgressHUD [from here: https://github.com/jdg/MBProgressHUD] (I've take out the excess code; this is just what you would need to implement my way to use it globally):

 

There is a slight drawback; if you do multiple operations where each displays the HUD, and one finishes, the HUD will be hidden and you won't know that the other operation is still going. I haven't actively looked at trying to run the operations one after another but this works great for me either way.

 

AppDelegate.h

#import "MBProgressHUD.h"

@interface iServiceAppDelegate : UIResponder <UIApplicationDelegate>
{
	MBProgressHUD *HUD;
}

@property (nonatomic, retain) MBProgressHUD *HUD;

+ (MBProgressHUD *)showGlobalProgressHUD;
+ (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title;
+ (void)dismissGlobalHUD;

@end

AppDelegate.m

@synthesize HUD;

+ (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title {
    UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
    [MBProgressHUD hideAllHUDsForView:window animated:YES];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
	if (title != nil && ![title isEqualToString:@""])
	{
		hud.labelText = title;
    }
	
	return hud;
}

+ (MBProgressHUD *)showGlobalProgressHUD {
	return [self showGlobalProgressHUDWithTitle:nil];
}

+ (void)dismissGlobalHUD {
    UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
    [MBProgressHUD hideHUDForView:window animated:YES];
}

In my *-Prefix.pch file, I have these defined:

#define ShowHUD								[AppDelegate showGlobalProgressHUD]
#define ShowHUDwithTitle(title)						[AppDelegate showGlobalProgressHUDWithTitle:title]
#define HideHUD								[AppDelegate dismissGlobalHUD]

And then I implement it like so:

+ (void)syncUnitOfMeasures
{
	ShowHUDwithTitle(@"Syncing Unit of Measures...");
	
	// Create the web service definition for retrieving customers.
    SCWebServiceDefinition *webServiceDef = [SCWebServiceDefinition
											 definitionWithBaseURL:WEB_SERVICE_URL
											 fetchObjectsAPI:@"GetUnitOfMeasures" resultsKeyName:@"GetUnitOfMeasuresResult"
											 resultsDictionaryKeyNamesString:@"UNIT_OF_MEASURE;DESCRIPTION;ROWID"];
	[webServiceDef.fetchObjectsParameters setValue:Username forKey:@"username"];
	[webServiceDef.fetchObjectsParameters setValue:Password forKey:@"password"];
	
	SCWebServiceStore *webServiceStore = (SCWebServiceStore *)[webServiceDef generateCompatibleDataStore];
	
	[webServiceStore asynchronousFetchObjectsWithOptions:nil
												 success: ^(NSArray *results)
	 {
		 NSLog(@"Successfully fetched %i objects!", results.count);
		 
		 if (results.count == 0) {
                         // Hide the HUD if no records were returned.
			 HideHUD;
		 } else {
			 NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
			 [fetch setEntity:[NSEntityDescription entityForName:@"UnitOfMeasure" inManagedObjectContext:managedContext]];
			 [fetch setIncludesPropertyValues:NO]; //only fetch the managedObjectID
			 
			 NSError *error = nil;
			 
			 NSArray *uoms = [managedContext executeFetchRequest:fetch error:&error];
			 
			 for (NSManagedObject *uom in uoms) {
				 [managedContext deleteObject:uom];
			 }
			 
			 error = nil;
			 if (![managedContext save:&error])
			 {
				 NSLog(@"Error - could not delete unit of measures list: %@", [error localizedDescription]);
			 }
			 else
			 {
				 NSLog(@"Deleted existing unit of measures list.");
			 }
			 
			 [results enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
				 
				 NSLog(@"------------------------------------------------------");
				 
				 NSError *error;
				 
				 NSLog(@"Inserting new record for uom : %@ - %@ (%i of %i)", [obj objectForKey:@"UNIT_OF_MEASURE"], [obj objectForKey:@"DESCRIPTION"], idx+1, results.count);
				 
				 UnitOfMeasure *uom = [NSEntityDescription
								   insertNewObjectForEntityForName:@"UnitOfMeasure"
								   inManagedObjectContext:managedContext];
				 
				 uom.uomCode = [obj objectForKey:@"UNIT_OF_MEASURE"];
				 uom.unitOfMeasure = [obj objectForKey:@"DESCRIPTION"];
				 uom.rowId = [obj objectForKey:@"ROWID"];
				 
				 if (![managedContext save:&error]) {
					 NSLog(@"Error - could not insert unit of measure: %@", [error localizedDescription]);
				 }
			 }];
			 
			 NSLog(@"Finished updating unit of measures list.");
			 NSLog(@"------------------------------------------------------");
			 
                         // All done; hide the HUD.
			 HideHUD;
		 }
	 }
												 failure: ^(NSError *error)
	 {
		 NSLog(@"Failed fetching objects due to: %@", error);

                 // Hide the HUD here after receiving an error.
		 HideHUD;
	 }];
}

I show it at the beginning and then I need to hide them in three different places - once I'm done processing successfully or receive an error or no records returned.

 

Hope it helps!

 

Wg


Edited by wizgod, 27 May 2013 - 08:48 PM.

P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)


#3 Balachandhar

Balachandhar

    Experienced Member

  • STV 3.0 Pro
  • PipPipPipPip
  • 87 posts
Reputation: 21
Excellent

Posted 27 May 2013 - 10:33 PM

thanks wizgod,

 

my question is something different than your answer. i want know those three places to show and hide the activity indicator when i use SCArrayOfObjectsModel/SCArrayOfObjectsSection.

 

eg.: something like these downloadWillStart, downloadDidFinish, downloadDidFinishWithError 

 

thanks for your detailed explanation for showing globally, 



#4 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 575 posts
  • LocationThe Grid
Reputation: 149
Popular

Posted 28 May 2013 - 04:28 AM

No worries, for the SCArrayOfObjectsSection, I was using it like this:

 

    // Show the HUD before starting the operation.
    ShowHUD;

    // Create the web service definition for retrieving topics.
    SCWebServiceDefinition *topicDef = [SCWebServiceDefinition
                                           definitionWithBaseURL:WEB_SERVICE_URL
                                           fetchObjectsAPI:[NSString stringWithFormat:@"topics/%@",categoryId] resultsKeyName:@"topic"
                                           resultsDictionaryKeyNamesString:@"topicId, categoryId, subject, creatorName, replies, dateCreated, lastMessageDate, avatar"];

	SCArrayOfObjectsSection *section = [SCArrayOfObjectsSection sectionWithHeaderTitle:categoryName webServiceDefinition:topicDef];
	
	section.sectionActions.fetchItemsFromStoreFailed = ^(SCArrayOfItemsSection *itemsSection, NSError *error)
	{
		NSLog(@"Failed to fetch items: %@", error.description);
		HideHUD;
	};
	
	section.sectionActions.didFetchItemsFromStore = ^(SCArrayOfItemsSection *itemsSection, NSMutableArray *items)
	{
		NSLog(@"Items fetched: %i", items.count);
		
		NSLog(@"Hide HUD");
		HideHUD;
	};

 

I don't know of an action for the beginning of the fetch but I just show the HUD before I start the operation.

 

Hope that helps.

 

Wg


Edited by wizgod, 28 May 2013 - 04:34 AM.

  • Balachandhar likes this

P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)


#5 Balachandhar

Balachandhar

    Experienced Member

  • STV 3.0 Pro
  • PipPipPipPip
  • 87 posts
Reputation: 21
Excellent

Posted 28 May 2013 - 10:39 AM

Thanks wizgod,

 

So for SCArrayOfObjectsModel there is no way to identify the start and end event for the data fetch operation? 

 

thanks,

bala



#6 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 575 posts
  • LocationThe Grid
Reputation: 149
Popular

Posted 28 May 2013 - 01:46 PM

Just replace the section with objectsModel:

 

	SCArrayOfObjectsModel *objectsModel = [[SCArrayOfObjectsModel alloc] initWithTableView:self.tableView entityDefinition:priceListDef];
	
	objectsModel.sectionActions.fetchItemsFromStoreFailed = ^(SCArrayOfItemsSection *itemsSection, NSError *error)
	objectsModel.sectionActions.didFetchItemsFromStore = ^(SCArrayOfItemsSection *itemsSection, NSMutableArray *items)

 

Wg


P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)


#7 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 575 posts
  • LocationThe Grid
Reputation: 149
Popular

Posted 24 June 2013 - 08:22 PM

If I had more than one section and I was hiding the progress hud in the objectsModel.sections.fetchItemsFromStore, the hud would hide after the first section was retrieved and I didn't want that.

 

I found a solution thanks to Everett's help here: http://sensiblecocoa...hitemsfromstore

 

objectsModel.sectionActions.didFetchItemsFromStore = ^(SCArrayOfItemsSection *itemsSection, NSMutableArray *items)
{
	NSLog(@"%i items fetched! Current section: %i of %i", items.count, [itemsSection.ownerTableViewModel indexForSection:itemsSection], objectsModel.sectionCount);
	
	if(![[items objectAtIndex:0] isKindOfClass:[SCFetchItemsCell class]] && [itemsSection.ownerTableViewModel indexForSection:itemsSection] == objectsModel.sectionCount - 1)
	{
		HideHUD;
	}
};

 

Now it'll hide the hud after all sections have been fetched.

 

Wg


Edited by wizgod, 24 June 2013 - 08:23 PM.

P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)






Also tagged with one or more of these keywords: models, sections, actions, view-controllers, web-services

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users