redoc01
redoc01

Reputation: 2325

Objective-c - Calling Latest Data from Database using Timestamp or actual Date

I have an App that allows the user to reload latest messages, so what i have is a PHP Script that gets the latest data selects all the new messages after or equal to the latest date.

My question is do i compare the TimeStamp (could be 1402382737) or do i compare the actual date. Ill go into code now:

Here is the SQL that Selects the latest messages:

$sql = "select m.*, u.*, at.*, UNIX_TIMESTAMP(M_Date) as M_Date2 from M_Messages m inner join AT_AddThread at on at.AT_ID = m.M_AT_ID inner join U_Users u on u.U_ID = m.M_U_ID where M_AT_ID = :idThread and m.M_Date >= :lastDate order by m.M_Date ASC";

Here is the Objective-C Code: that sets the latest date:

    if((NSNull *)[dict objectForKey:@"M_Date2"] != [NSNull null]){
        NSString *dateTS = [dict objectForKey:@"M_Date"];
        NSString *timestampString = [dict objectForKey:@"M_Date2"];
        double timestampDate = [timestampString doubleValue];
        NSDate *d = [NSDate dateWithTimeIntervalSince1970:timestampDate];
        NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
        [_formatter setDateFormat:@"dd.MM.yyyy HH:mm:ss"];
        m_Date = [_formatter stringFromDate:d];
        if (counter == 1) {
        lastDatePostActivity = dateTS;
        }
    }

Do i send the TimeStamp back to the PHP Script or the actual Date, and if i send back the TimeStamp do i need to modify my select statement to this:

$sql = "select m.*, u.*, at.*, UNIX_TIMESTAMP(M_Date) as M_Date2 from M_Messages m inner join AT_AddThread at on at.AT_ID = m.M_AT_ID inner join U_Users u on u.U_ID = m.M_U_ID where M_AT_ID = :idThread and UNIX_TIMESTAMP(m.M_Date) >= :timestamp order by m.M_Date ASC";

Because the app is going to have Universal Times so if another user from another country sends a message then all the timezones should match what every country you are in, thats why i am unsure whether to use the timestamp or the actual date

Upvotes: 1

Views: 63

Answers (1)

ogres
ogres

Reputation: 3690

You must not use NSDate object from client , users might have different time zones or incorrect time ,

When you fetch data first time from your backend , you must also provide current server-time and save it , next time when you want to fetch something from backend you will provide this ( saved ) timestamp and backend will filter messages ,

if you do not want to save this timestamp , you can always check in current messages , what is the maximum timestamp for messages ( again , these timestamps should be generated by server ) and use it ,

so imagine you have 2 methods , one that fetches data from backend , and another that returns last-update-date on current client device,

you can use timestamps directly and convert timestamp via php into date object or string ( the same format that is used by your SQL database )

for example

$date = date('Y-m-d H:i:s',$timestamp);

steps .

  1. Create the function that will return the last-update-date on client
  2. Fetch messages using that date ( if this is the first time user wants to fetch data , then use timestamp 0 )
  3. Correctly save last-update-date timestamp , do not store NSDate , it might be incorrect if user changes date and you use it improperly.

Here is an easy example

    // php example
    echo json_encode(Array( 'data' => $yourData, 'timestamp' => time() ));

    //objc example
    - (NSNumber *)lastTimestamp {
            NSNumber *timestamp = [[NSUserDefaults standardUserDefaults] objectForKey:@"timestamp"];
            if(!timestamp) timestamp = @(0);
            return timestamp;
    }


    NSDictionary *result = fetchDataFromBackendWithTimestamp([self lastTimestamp]);
    // Save messages


    NSNumber *timestamp = result[@"timestamp"];
    [[NSUserDefaults standardUserDefaults] setObject:timestamp forKey:@"timestamp"];
    [[NSUserDefaults standardUserDefaults] synchronize];


// Php SQL Query will be somethng like this
$sql = "SELECT * FROM YOUR_TABLE WHERE date_field > :date_field ";
$statement = $mysql->prepare($sql);
$date = date('Y-m-d H:i:s',$timestamp);
$statement->bindParam(":date_field",$date);
$statement->execute();

while($row = $statement->fetch(PDO::FETCH_ASSOC)) {
    // do something with the row
}    

Upvotes: 2

Related Questions