DACachedCalculatedFieldDefinition

Overview

DACachedCalculatedFieldDefinition class represents a special kind of the calculated field with the ability to cache its value. This allows to significantly improve the performance by reducing unnecessary calls to the calculation method.

You can create cached calculated field using addCalculatedFieldName:dataType:target:selector:cached: method of the DADataTable class

Declaring cached calculated field in the DADataTable will cause adding several additional internal fields which will serve such kind of fields:

a) ___calculatedField_YOUR_FIELD_NAME field stores the cached value.

b) ___calculatedFieldVersion_YOUR_FIELD_NAME which contains the current version number for the cached value. If the version stored in the row equals to version in the field definition instance then cached value will be returned. Otherwise, the calculation method will be called and updated value will be returned.

Cache values are stored only in memory and will not be serialized neither to the stream to the server nor into a briefcase.

At the same time you can manually control the moment when you will need to refresh the cached value. When you call clearCache method then it will increase its version number and the next attempt of getting calculate field will lead to executing calculate method and refreshes the value.

Sample of using DACachedCalculatedFieldDefinition class:


- (id)calculateCurrentTime:(DADataTableRow *)row {
    NSLog(@"Calculating new value for 'Time' field...");
    return [NSDate date];
}

- (void)testCachedCalcField:(DADataTable *)table {
    
    DACalculatedFieldDefinition *fieldDef = 
            [table addCalculatedFieldName:@"Time" 
                                 dataType:datDateTime 
                                   target:self 
                                 selector:@selector(calculateCurrentTime:)
                                   cached:YES];
    
    NSDateFormatter * fmt = [[[NSDateFormatter alloc] init] autorelease];
    [fmt setDateStyle:kCFDateFormatterNoStyle];
    [fmt setTimeStyle:kCFDateFormatterFullStyle];
    
    NSDate *value = [[[table rows] objectAtIndex:1] valueForKey:@"Time"];
    NSLog(@"Current value is %@", [fmt stringFromDate:value]);
    
    NSLog(@"Sleeping for 2 seconds");
    [NSThread sleepForTimeInterval:2.0];
    
    value = [[[table rows] objectAtIndex:1] valueForKey:@"Time"];
    NSLog(@"Value is still %@", [fmt stringFromDate:value]);
    
    NSLog(@"Clearing the cached value");
    [(DACachedCalculatedFieldDefinition *)fieldDef clearCache];
    
    value = [[[table rows] objectAtIndex:1] valueForKey:@"Time"];
    NSLog(@"Value is %@", [fmt stringFromDate:value]);

    NSLog(@"Again sleep for 2 seconds");
    [NSThread sleepForTimeInterval:2.0];
    
    value = [[[table rows] objectAtIndex:1] valueForKey:@"Time"];
    NSLog(@"Value is still %@", [fmt stringFromDate:value]);
    
}

and the output: Calculating new value for 'Time' field... Current value is 12:34:55 Sleeping for 2 seconds Value is still 12:34:55 Clearing the cached value Calculating new value for 'Time' field... Value is 12:34:57 Again sleep for 2 seconds Value is still 12:34:57

Location

 

clearCache

Increases version number and thus make the current cached value invalid. During the next attempt of getting calculated value, the cache will be updated with the fresh value

- (void) clearCache

dataType    (declared in DAFieldDefinition)

Specifies the Data Type for the field.

@property (readonly) enum DADataType dataType

dataTypeName    (declared in DAFieldDefinition)

Returns the readable name of the fields data type, as defined by the dataType property.

@property (readonly) NSString *dataTypeName

initWithDataTable:name:dataType:target:selector:    (declared in DACalculatedFieldDefinition)

Initializes a new instance of a calculated field.

- (id) initWithDataTable:(DADataTable *)ownerTable name:(NSString *)fieldName dataType:(enum DADataType)dataType target:(id)target selector:(SEL)selector

Parameters:

  • ownerTable: Specifies the DADataTable this field will be defined for.
  • fieldName: Specifies the unique name of this field within the data table.
  • dataType: Specifies the Data Type for the newly calculated field.
  • target: Specifies the object that implements the calculation method identified by the selector.
  • selector: Specifies the selector identifying the method on the target that will calculate the value for this field.

initWithTable:    (declared in DAFieldDefinition)

Initializes new DAFieldDefinition instance with given data table

- (id) initWithTable:(DADataTable *)ownerTable

Parameters:


initWithTable:name:    (declared in DAFieldDefinition)

Initializes a new DAFieldDefinition instance with given data table and field name.

- (id) initWithTable:(DADataTable *)ownerTable name:(NSString *)fieldName

Parameters:

  • ownerTable:
  • fieldName:

name    (declared in DAFieldDefinition)

Specifies the uniqe name of the field within the data table. The field can be queried by this name using KVC's valueForKey: and setValue:forKey: methods on the individual DADataTableRows.

@property (readonly) NSString *name

position    (declared in DAFieldDefinition)

Returns the position or index of the field within the data table. This is mainly for internal use; regular field access should use the name and not rely on position.

@property (readonly) NSInteger position

predicateEditorRowTemplates    (declared in DAFieldDefinition)

Returns an array with one or more NSPredicateEditorRowTemplates that match the field definition, for use in an NSPredicateEditor. Usually, you will not call 'send this message' to an individual field, but use the DADataTable's defaultPredicateEditorRowTemplates method. See also Working with NSPredicateEditor (Xcode (Mac)).

- (NSArray *) predicateEditorRowTemplates

properties    (declared in DAFieldDefinition)

Provides access to various properties of the field as defined in the server's Schema.

@property (readonly) NSDictionary *properties

selector    (declared in DACalculatedFieldDefinition)

Specifies the object that implements the calculation method identified by the selector.

@property (readonly) SEL selector

setProperty:toValue:    (declared in DAFieldDefinition)

Updates a property stored in the properties list.

- (void) setProperty:(NSString *)propertyName toValue:(NSString *)propertyValue

Parameters:

  • propertyName: name of the property for which we want to set new value
  • propertyValue: new value we want to set.

table    (declared in DAFieldDefinition)

Returns a reference to the DADataTable that contains this field.

@property (readonly) DADataTable *table

target    (declared in DACalculatedFieldDefinition)

Specifies the selector identifying the method on the target that will calculate the value for this field.

@property (readonly) id target

valueForRow:    (declared in DACalculatedFieldDefinition)

For internal use, returns the calculated value for a given row, by performing the selector on the target.

- (id) valueForRow:(DADataTableRow *)row

Parameters:

  • row: Row to calculate the value for.

visible  assign    (declared in DAFieldDefinition)

Specifies whether this field should be visible in user interfaces. This property is not used directly by the Data Abstract framework, but can be queried by user interface code to determine whether to see the field (for example in a table view or a field picker) or not. visible will be initialized based on the value set in the Schema, server-side, but can be changed within the client, if necessary.

@property (assign) BOOL visible

 

dataType    (declared in DAFieldDefinition)

Specifies the Data Type for the field.

@property (readonly) enum DADataType dataType

dataTypeName    (declared in DAFieldDefinition)

Returns the readable name of the fields data type, as defined by the dataType property.

@property (readonly) NSString *dataTypeName

name    (declared in DAFieldDefinition)

Specifies the uniqe name of the field within the data table. The field can be queried by this name using KVC's valueForKey: and setValue:forKey: methods on the individual DADataTableRows.

@property (readonly) NSString *name

position    (declared in DAFieldDefinition)

Returns the position or index of the field within the data table. This is mainly for internal use; regular field access should use the name and not rely on position.

@property (readonly) NSInteger position

properties    (declared in DAFieldDefinition)

Provides access to various properties of the field as defined in the server's Schema.

@property (readonly) NSDictionary *properties

selector    (declared in DACalculatedFieldDefinition)

Specifies the object that implements the calculation method identified by the selector.

@property (readonly) SEL selector

table    (declared in DAFieldDefinition)

Returns a reference to the DADataTable that contains this field.

@property (readonly) DADataTable *table

target    (declared in DACalculatedFieldDefinition)

Specifies the selector identifying the method on the target that will calculate the value for this field.

@property (readonly) id target

visible  assign    (declared in DAFieldDefinition)

Specifies whether this field should be visible in user interfaces. This property is not used directly by the Data Abstract framework, but can be queried by user interface code to determine whether to see the field (for example in a table view or a field picker) or not. visible will be initialized based on the value set in the Schema, server-side, but can be changed within the client, if necessary.

@property (assign) BOOL visible

 

clearCache

Increases version number and thus make the current cached value invalid. During the next attempt of getting calculated value, the cache will be updated with the fresh value

- (void) clearCache

initWithDataTable:name:dataType:target:selector:    (declared in DACalculatedFieldDefinition)

Initializes a new instance of a calculated field.

- (id) initWithDataTable:(DADataTable *)ownerTable name:(NSString *)fieldName dataType:(enum DADataType)dataType target:(id)target selector:(SEL)selector

Parameters:

  • ownerTable: Specifies the DADataTable this field will be defined for.
  • fieldName: Specifies the unique name of this field within the data table.
  • dataType: Specifies the Data Type for the newly calculated field.
  • target: Specifies the object that implements the calculation method identified by the selector.
  • selector: Specifies the selector identifying the method on the target that will calculate the value for this field.

initWithTable:    (declared in DAFieldDefinition)

Initializes new DAFieldDefinition instance with given data table

- (id) initWithTable:(DADataTable *)ownerTable

Parameters:


initWithTable:name:    (declared in DAFieldDefinition)

Initializes a new DAFieldDefinition instance with given data table and field name.

- (id) initWithTable:(DADataTable *)ownerTable name:(NSString *)fieldName

Parameters:

  • ownerTable:
  • fieldName:

predicateEditorRowTemplates    (declared in DAFieldDefinition)

Returns an array with one or more NSPredicateEditorRowTemplates that match the field definition, for use in an NSPredicateEditor. Usually, you will not call 'send this message' to an individual field, but use the DADataTable's defaultPredicateEditorRowTemplates method. See also Working with NSPredicateEditor (Xcode (Mac)).

- (NSArray *) predicateEditorRowTemplates

setProperty:toValue:    (declared in DAFieldDefinition)

Updates a property stored in the properties list.

- (void) setProperty:(NSString *)propertyName toValue:(NSString *)propertyValue

Parameters:

  • propertyName: name of the property for which we want to set new value
  • propertyValue: new value we want to set.

valueForRow:    (declared in DACalculatedFieldDefinition)

For internal use, returns the calculated value for a given row, by performing the selector on the target.

- (id) valueForRow:(DADataTableRow *)row

Parameters:

  • row: Row to calculate the value for.

See Also