开发者

can we make the user know the date on which the data updated from internet with iphone sdk?

开发者 https://www.devze.com 2023-01-18 04:33 出处:网络
i m making an iphone app that displays data from a website on a table view .basically i m atoring the website\'s data in an array and then display it on a table. now i want whenever the website update

i m making an iphone app that displays data from a website on a table view .basically i m atoring the website's data in an array and then display it on a table. now i want whenever the website update their data the user get the开发者_Python百科 date when the data was updated .can anybody tell some different ways to do it?


So, are you storing (caching) the website's data on the phone and only updating once in a while, or are to loading the website and populating the table every time the app starts?

If you are not storing it (with Core Data, User Defaults, NSKeyedArchiver, etc.) it will be difficult for how can you tell that the data has changed?

In that case (not storing) you could after having pulled the data from the website generate the hash value of the data and store it using NSUserDefaults along with a NSDate.

The next time you pull the information from the website you will generate the hash again and compare it to the previous value. If they match, then the content was not updated, else it was and you store the new hash value and the current date.

If you have control over the website you can do a couple of things.

  1. Return a HTTP header with information about when the website was last updated and compare it with a date stored locally. This will allow you to use the HEAD HTTP semantic to check if a full GET request is needed allowing you to minimize network access.

  2. Implement push notifications (does seem over-kill for your situation) telling the user that the content was updated.

Best of luck.

UPDATE

So your best bet would be calling [myArray hash] and store it in NSUserDefaults, e.g.

NSUInteger hashValue = [myArray hash];
NSDate *now = [NSDate now];

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:hashValue forKey:@"hash"];
[prefs setObject:now forKey:@"date"]; // Maybe you have to convert the date to a string.
[prefs synchronize];

And later load it when the app starts (or becomes active):

// Load the website again and generate the hash.
NSUInteger newHashValue = [myArray hash];

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSUInteger *oldHashValue = [prefs stringForKey:@"hash"];

NSDate *contentDate;

if (newHashValue == oldHashValue)
{
    contentDate = [prefs dateForKey:@"date"];
}
else
{
    contentDate = [NSDate now];

    // Store the new date.
    // and the new hash.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消