简体   繁体   English

Objective-C-使用时间戳或实际日期从数据库中调用最新数据

[英]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. 我有一个允许用户重新加载最新消息的应用程序,因此我拥有的是一个获取最新数据的PHP脚本,它选择了等于或等于最新日期的所有新消息。

My question is do i compare the TimeStamp (could be 1402382737) or do i compare the actual date. 我的问题是我要比较时间戳(可能是1402382737)还是要比较实际日期。 Ill go into code now: 我现在进入代码:

Here is the SQL that Selects the latest messages: 这是选择最新消息的SQL:

$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: 这是Objective-C代码:设置最新日期:

    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: 我是否将TimeStamp发送回PHP脚本或实际的日期,并且如果我将TimeStamp发送回,是否需要将select语句修改为此:

$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 因为该应用程序将具有世界时,所以如果另一个国家的另一个用户发送消息,那么所有时区都应与您所在的每个国家/地区相匹配,这就是为什么我不确定是否要使用时间戳或实际日期

You must not use NSDate object from client , users might have different time zones or incorrect time , 您不得使用来自客户端的NSDate对象,用户可能具有不同的时区或不正确的时间,

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, 假设您有2种方法,一种从后端获取数据,另一种返回当前客户端设备上的last-update-date,

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 ) 您可以直接使用时间戳,并通过php将时间戳转换为日期对象或字符串(与SQL数据库使用的格式相同)

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 ) 使用该日期获取消息(如果这是用户首次获取数据,则使用时间戳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. 正确保存last-update-date时间戳,不存储NSDate,如果用户更改日期并且使用不当,则可能不正确。

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
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM