Filters Sample (Xcode)

From RemObjects Wiki
Jump to: navigation, search

This is a Data Abstract for Xcode Samples topic





The DAFilters sample shows how to apply predicate conditions for either server-side or client-side filtering of data.

DAFiltersSampleOSX.png

Contents

Getting Started

Like all samples in Data Abstract for Xcode, this sample expects one of the PCTrade Sample Suite servers to be running on the network. Available servers will be discovered via ROZeroConf and can be selected from the Servers popup button in the sample's toolbar (if the popup button is empty, no servers were discovered).

NSPredicates are a very useful feature in Cocoa to express conditions for filtering data based on specific attributes – in the case of Data Abstract, specific field values. There are two ways predicates can be used:

For one, a predicate can be used to filter a set of data already present locally within a DADataTable. You can choose such mode for the sample by selecting Clientside (NSPredicate) from the Filter Mode popup. Next, you can select any of the predefined predicates from the Conditions popup. The sample will retrieve the full data table from the server, and then locally filter the data for display, by applying the selected predicate.

Secondly, the sample can perform server-side filtering, by generating a Dynamic Where clause from the NSPredicate statement, and transmitting it to the server. This is very useful when dealing with large sets of data, as the client can save network traffic (and be faster), by only downloading the data it needs. To try this option, change Filter Mode to Serverside (DADynWhere) and try to apply the same conditions. When you check the log file in the server application, you will see that each condition will be translated into an appropriate Dynamic Where clause, and included into getData request for the server.


Once data is retrieved, you will notice that additional commands appear in the Conditions popup, allowing you to adjust the existing conditions, or create new ones. These leverage the NSPredicateEditor window provided by Cocoa, to provide a rich editing experience that will be intuitive for non-technical users. Data Abstract's DADataTable class provides functionality to automatically populate an NSPredicateEditor with the necessary information to work with the specific table (including, if present, lookup fields).

In addition to the NSPredicateEditor, the sample also provides a plain text view of the conditions, if you click the Filter Text tab, instead of Filter Editor. You can play around with different conditions in either tab, to see how one affects the other.

Examine the code

The main part of work in this sample happens in the fill: method:

-(void)fill:(id)sender
{
	DAAsyncRequest* request = nil;
	[self setupRda];
	...
	currentConditionName = [sender title];
	NSPredicate *p = [conditionsByName objectForKey:currentConditionName];
	[predicateLabel setStringValue:[p description]];
	
	if ([modePopup indexOfSelectedItem] == 1) 
	// Server-side filtering...
	{
		NSString *dynWhereXml = [DADynamicWhereXmlGenerator generateXmlFromPredicate:p];
		DADynamicWhereClause *clause = [DADynamicWhereClause dynamicWhereClauseWithXmlString:dynWhereXml];
		request = [rda beginGetDataTable:@"Workers" 
								  select:[self fieldsToSelect] 
								   where:clause
								   start:NO];
		[request setDelegate:self];
		[request start];
	}
	
	else 
	// client-side filtering...
	{
		rows = [[[workersTable rows] filteredArrayUsingPredicate:p] retain];
		[tableView reloadData];
		...
	}
}


Editions and Languages

Concepts Covered


See Also


Product: RemObjects Data Abstract
Available Editions: Data Abstract for .NET, Xcode, Delphi, Java and JavaScript

GlossaryArticlesFeaturesLibrarySamples

Personal tools
Namespaces

Variants
Actions
Navigation
products
platforms
special
Toolbox