最近有个新需求,就是做下电商App基本都有的功能-足迹,简单的说不就是浏览记录嘛。设计图是下面这样滴!
看上去设计图也很正常,该有的都有了,只要数据没问题,基本就是写下界面就over了,万万没想到,问题就出现在这数据上,所有数据竟然是客户端自己存,自己取,完完全全的一个本地浏览记录啊😭,既然需求确定是这样了,那就没办法只能开始了。
存数据
数据怎么存呢?下面几种数据持久化方案都行:
- plist文件
- NSUserDefaults
- NSKeyedArchiver
- 数据库
- CoreData
这里由于赶时间就偷了个懒,选用了NSUserDefaults 来存储数据(应该首选数据库和CoreData)。
然后需要存一些什么数据呢?业务需要,我们每条数据都需要有commodityId(商品id),commodityInfoId(商品信息sku),scanTime(商品浏览时间)。由于商品id是唯一的,这里我就采用了字典key的唯一性来进行去重,只保留最新的浏览记录。
- (NSMutableDictionary *)commodityDic {
if (!_commodityDic) {
_commodityDic = [NSMutableDictionary dictionaryWithDictionary:[self.storageDic objectForKey:@"用户userId"];
}
return _commodityDic;
}
- (NSMutableArray *)commodityArray {
if (!_commodityArray) {
_commodityArray = [NSMutableArray array];
}
return _commodityArray;
}
NSString *insetStr = [NSString stringWithFormat:@"%@|%@", scanTime,commodityInfoId];
[self.commodityDic setObject:insetStr forKey:commodityId];
[self.storageDic setObject:self.commodityDic forKey:@"用户userId"];
[[NSUserDefaults standardUserDefaults] setObject:self.storageDic forKey:@"你的key"];
[[NSUserDefaults standardUserDefaults] synchronize];
//两个字典的结构是这样的:
self.commodityDic {
ZMCOMD132170419142301330 = "2017-11-14 17:56:49|ZMCOMD132170419142301330170419142301304";
ZMCOMD426170808141848800 = "2017-11-14 15:05:35|ZMCOMD426170808141848800170808141848472";
ZMCOMD881170710143358733 = "2017-11-14 17:48:46|ZMCOMD881170710143358733170710143358515";
ZMCOMD956171018164822665 = "2017-11-14 17:48:39|ZMCOMD956171018164822665171018164822894";
}
self.storageDic
{
16050368695640 = {
ZMCOMD233170802151805643 = "2017-11-14 10:30:02|647060";
ZMCOMD593170208112702710 = "2017-11-14 10:29:57|ZMCOMD593170208112702710170208112702912";
};
17091455092851 = {
ZMCOMD132170419142301330 = "2017-11-14 17:56:49|ZMCOMD132170419142301330170419142301304";
ZMCOMD426170808141848800 = "2017-11-14 15:05:35|ZMCOMD426170808141848800170808141848472";
ZMCOMD881170710143358733 = "2017-11-14 17:48:46|ZMCOMD881170710143358733170710143358515";
ZMCOMD956171018164822665 = "2017-11-14 17:48:39|ZMCOMD956171018164822665171018164822894";
};
}
好了,数据这样就算是存上了😀。
取数据
//根据用户id来取数据
NSMutableDictionary *disorderlyDic = [self.storageDic objectForKey@"用户userId"];
NSArray *keysArray = disorderlyDic.allKeys;
NSArray *valuesArray = disorderlyDic.allValues;
//按时间进行排序
NSArray *sortArray = [valuesArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj2 compare:obj1 options:NSNumericSearch];
}];
NSMutableArray *orderlyArray = [NSMutableArray array];
for (NSInteger i = 0; i < sortArray.count; i++) {
NSInteger index = [valuesArray indexOfObject:sortArray[i]];
NSString *key = keysArray[index];
NSString *value = sortArray[i];
[orderlyArray addObject:@{key:value}];
}
//按时间排序后的数据是这样的
sortArray
<__NSArrayI 0x60800024f9f0>(
2017-11-14 17:56:49|ZMCOMD132170419142301330170419142301304,
2017-11-14 17:48:46|ZMCOMD881170710143358733170710143358515,
2017-11-14 17:48:39|ZMCOMD956171018164822665171018164822894,
2017-11-14 15:05:35|ZMCOMD426170808141848800170808141848472
)
orderlyArray
<__NSArrayM 0x60800005b7e0>(
{
ZMCOMD132170419142301330 = "2017-11-14 17:56:49|ZMCOMD132170419142301330170419142301304";
},
{
ZMCOMD881170710143358733 = "2017-11-14 17:48:46|ZMCOMD881170710143358733170710143358515";
},
{
ZMCOMD956171018164822665 = "2017-11-14 17:48:39|ZMCOMD956171018164822665171018164822894";
},
{
ZMCOMD426170808141848800 = "2017-11-14 15:05:35|ZMCOMD426170808141848800170808141848472";
}
)
数据合并,排序
//将时间相同的合并在一起
NSDictionary *commodityAllDic = [NSMutableDictionary dictionary];
for (NSInteger i = 0; i < orderlyArray.count; i ++) {
NSDictionary *comDic = self.commodityArray[i];
NSString *comTime = [[comDic.allValues[0] componentsSeparatedByString:@"|"][0] substringWithRange:NSMakeRange(0, 10)];
NSMutableArray *array = [self.commodityAllDic objectForKey:comTime];
if (!array) {
array =[NSMutableArray array];
}
[array addObject:comDic.allKeys[0]];
[commodityAllDic setObject:array forKey:comTime];
}
//按时间排序
NSArray *keyArray = [self.commodityAllDic allKeys];
NSArray *sortArray = [keyArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj2 compare:obj1 options:NSNumericSearch];
}];
//数据结构是这样的
commodityAllDic
{
"2017-11-14" = (
ZMCOMD132170419142301330,
ZMCOMD881170710143358733,
ZMCOMD956171018164822665,
ZMCOMD426170808141848800
);
}
sortArray
<__NSSingleObjectArrayI 0x60800001d7c0>(
2017-11-14
)
做到这一步数据处理基本完事了,有sortArray,我们可以取到commodityAllDic对应的commodityId,根据commodityId,我们可以取到存取的数据。
删除数据
这个就简单了
- (void)removeCommodityWithCommodityId:(NSMutableArray *)commodityIdArray {
for (NSString *commodityId in commodityIdArray) {
[self.commodityDic removeObjectForKey:commodityId];
}
[self.storageDic setObject:self.commodityDic forKey:@"用户userId"];
[[NSUserDefaults standardUserDefaults] setObject:self.storageDic forKey:@"你的key"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
分页加载数据
上面的几个都搞定了,这个也比较简单了,确定每一页需要显示的个数,从排序后orderlyArray中依次取出相对应的个数,然后按照数据合并,排序的方法,来进行整合数据。