none
Count the number of rows returned by a query, using Azure mobile client library for iOS

    Question

  • As far as I can tell, it seems impossible to do something analagous to "Select count(*) ...." using the azure mobile client library for iOS.

    Yes, I know "TotalCount" can be used to get the total number of rows in the table -- not what I want.

    Yes, I know I can retrieve a minimal column subset, and then count the number of rows returned -- not what I want.

    In my instance, I don't need any of the actual row/column values, and forcing me to retrieve them over a limited-bandwidth mobile device connection instead of the one number I am actually interested in, is crazy.  Well, it is inefficient, and a glaring oversight.  This is a basic SQL operation, and it needs to have a solution that is efficient, and not a hacky work-around.

    PLEASE ADDRESS THIS -- and, if all my fruitless searches so far for a solution to this have been incorrect, and there is already a solution that I haven't found, apologies in advance.  (I'm a little frustrated right now!)


    • Edited by Terry_SD Thursday, March 20, 2014 3:28 PM
    Thursday, March 20, 2014 3:23 PM

Answers

  • After you create your MSQuery and set it's includeTotalCount property to YES, set it's fetchLimit property to 0.  This should send a query to the server where you're asking for the total count but not actually pulling back any of the rows.  Alternatively you could alter the READ script or create a custom API which would do the same thing on the server side if you don't want to specify it on the client side.
    Thursday, March 20, 2014 3:37 PM

All replies

  • After you create your MSQuery and set it's includeTotalCount property to YES, set it's fetchLimit property to 0.  This should send a query to the server where you're asking for the total count but not actually pulling back any of the rows.  Alternatively you could alter the READ script or create a custom API which would do the same thing on the server side if you don't want to specify it on the client side.
    Thursday, March 20, 2014 3:37 PM
  • Suggested implementation:

    typedef void (^MSCountQueryBlock)(NSInteger resultCount,
                                     NSError *error);

    /// Sends a request to the Windows Azure Mobile Service to return the row count
    /// from the table that meet the conditions of the given predicate.
    -(void)countRowsWithPredicate:(NSPredicate *) predicate
          completion:(MSCountQueryBlock)completion;

    Thursday, March 20, 2014 3:43 PM
  • TotalCount is the number of rows in the table, NOT the number of rows that match the query.

    Direct quote from the API doc:  

    includeTotalCount

    Indicates if the Windows Azure Mobile Service should also include the total count of items on the server (not just the count of items returned) with the query results.


    • Edited by Terry_SD Thursday, March 20, 2014 4:21 PM
    Thursday, March 20, 2014 4:19 PM
  • The total count IS the number of rows that match the query and NOT the total rows in the table.  I just tested it out and even if you use a fetchLimit of 0, the totalCount field will still be set to the number of rows that match the query.  I'll suggest that the documentation be altered to better reflect that.
    Thursday, March 20, 2014 8:28 PM