Skip to content

Examples

Kjell Nilsson edited this page Oct 4, 2018 · 3 revisions

Examples

How to fetch all rows of an entity

    var fetchSpecification = [LOFetchSpecification fetchSpecificationForEntityNamed:@"Person" qualifier:nil];
    [objectContext requestObjectsWithFetchSpecification:fetchSpecification withCompletionHandler:function(objects, statusCode) {
        if (statusCode === 200) {
            // Do something with the objects
        }
    }];

How to add an operator to the fetch

An operator is extra information that is sent to the backend. It is used for example to tell the backend to do a lazy fetch (just send the primary keys) or count the number of objects (just send a number with the number of rows). The default behavior that is used with the objc-backend is to add the operator after the method in the url. Normal url for a fetch is http://localhost/backend/fetch/Person. If a lazy operator is added the url will look like http://localhost/backend/fetch/lazy/Person

    var fetchSpecification = [LOFetchSpecification fetchSpecificationForEntityNamed:@"Person" qualifier:nil];
    [fetchSpecification setOperator:@"lazy"];

    [objectContext requestObjectsWithFetchSpecification:fetchSpecification withCompletionHandler:function(objects, statusCode) {
        if (statusCode === 200) {
            // Do something with the objects
        }
    }];

How to use a special method (function) in the backend to fetch objects

The default behavior that is used with the objc-backend is to use the method fetch to fetch objects. Normal url for a fetch is http://localhost/backend/fetch/Person. If a special method is implemented in the backend to send objects to the client the url will look like http://localhost/backend/MyMethod/Person. This is how to tell the LOF to use an alternative method to fetch the objects

    var fetchSpecification = [LOFetchSpecification fetchSpecificationForEntityNamed:@"Person" qualifier:nil];
    [fetchSpecification setMethod:@"MyMethod"];

    [objectContext requestObjectsWithFetchSpecification:fetchSpecification withCompletionHandler:function(objects, statusCode) {
        if (statusCode === 200) {
            // Do something with the objects
        }
    }];

How I can alter the request before it is sent to the backend.

    var fetchSpecification = [LOFetchSpecification fetchSpecificationForEntityNamed:@"Person" qualifier:nil];

    [fetchSpecification setRequestPreProcessBlock:function(request) {
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:[CPString JSONFromObject:{
            sessionKey: theSessionKey,
            itemKeys: itemKeys
        }]];
    }];

    [objectContext requestObjectsWithFetchSpecification:fetchSpecification withCompletionHandler:function(objects, statusCode) {
        if (statusCode === 200) {
            // Do something with the objects
        }
    }];

Clone this wiki locally