| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- //
- // MessageStatusManager.m
- // AIIM
- //
- // Created by AI Assistant on 2025/10/21.
- //
- #import "MessageStatusManager.h"
- // plist 文件名
- static NSString *const kMessageStatusPlistName = @"MessageStatus.plist";
- @interface MessageStatusManager ()
- @property (nonatomic, strong) NSMutableDictionary<NSString *, NSNumber *> *statusCache;
- @property (nonatomic, strong) dispatch_queue_t ioQueue;
- @property (nonatomic, copy) NSString *plistPath;
- @end
- @implementation MessageStatusManager
- #pragma mark - Singleton
- + (instancetype)sharedInstance {
- static MessageStatusManager *instance = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- instance = [[self alloc] init];
- });
- return instance;
- }
- - (instancetype)init {
- self = [super init];
- if (self) {
- // 创建串行队列用于文件读写,避免并发问题
- _ioQueue = dispatch_queue_create("com.aiim.messageStatus.io", DISPATCH_QUEUE_SERIAL);
-
- // 获取 plist 文件路径
- _plistPath = [self plistFilePath];
-
- // 加载缓存
- [self loadCache];
- }
- return self;
- }
- #pragma mark - Public Methods
- - (void)saveMessageStatus:(NSInteger)status forMessageId:(NSString *)messageId {
- if (!messageId || messageId.length == 0) {
- NSLog(@"[MessageStatusManager] 保存失败: messageId 为空");
- return;
- }
-
- dispatch_async(self.ioQueue, ^{
- // 更新内存缓存
- self.statusCache[messageId] = @(status);
-
- // 保存到文件
- [self saveCacheToDisk];
-
- NSLog(@"[MessageStatusManager] 保存成功: messageId=%@, status=%ld", messageId, (long)status);
- });
- }
- - (NSInteger)getMessageStatusForMessageId:(NSString *)messageId {
- if (!messageId || messageId.length == 0) {
- NSLog(@"getMessageStatusForMessageId 1: 1");
- return 1; // 默认返回发送成功
- }
-
- if (![self.statusCache.allKeys containsObject:messageId]) {
- NSLog(@"getMessageStatusForMessageId 2: 1");
- return 0;
- }
-
- __block NSInteger status = 0; // 默认值
- dispatch_sync(self.ioQueue, ^{
- NSNumber *statusNumber = self.statusCache[messageId];
- if (statusNumber) {
- status = [statusNumber integerValue];
- }
- });
- NSLog(@"getMessageStatusForMessageId 3: %ld", status);
- return status;
- }
- - (void)batchSaveMessageStatus:(NSDictionary<NSString *, NSNumber *> *)statusDict {
- if (!statusDict || statusDict.count == 0) {
- return;
- }
-
- dispatch_async(self.ioQueue, ^{
- // 批量更新内存缓存
- [self.statusCache addEntriesFromDictionary:statusDict];
-
- // 保存到文件
- [self saveCacheToDisk];
-
- NSLog(@"[MessageStatusManager] 批量保存成功: %lu 条", (unsigned long)statusDict.count);
- });
- }
- - (NSDictionary<NSString *, NSNumber *> *)batchGetMessageStatusForMessageIds:(NSArray<NSString *> *)messageIds {
- if (!messageIds || messageIds.count == 0) {
- return @{};
- }
-
- __block NSMutableDictionary *result = [NSMutableDictionary dictionary];
- dispatch_sync(self.ioQueue, ^{
- for (NSString *messageId in messageIds) {
- NSNumber *status = self.statusCache[messageId];
- if (status) {
- result[messageId] = status;
- } else {
- result[messageId] = @(1); // 默认发送成功
- }
- }
- });
-
- return [result copy];
- }
- - (void)removeMessageStatusForMessageId:(NSString *)messageId {
- if (!messageId || messageId.length == 0) {
- return;
- }
-
- dispatch_async(self.ioQueue, ^{
- [self.statusCache removeObjectForKey:messageId];
-
- // 保存到文件
- [self saveCacheToDisk];
-
- NSLog(@"[MessageStatusManager] 删除成功: messageId=%@", messageId);
- });
- }
- - (void)batchRemoveMessageStatusForMessageIds:(NSArray<NSString *> *)messageIds {
- if (!messageIds || messageIds.count == 0) {
- return;
- }
-
- dispatch_async(self.ioQueue, ^{
- [self.statusCache removeObjectsForKeys:messageIds];
-
- // 保存到文件
- [self saveCacheToDisk];
-
- NSLog(@"[MessageStatusManager] 批量删除成功: %lu 条", (unsigned long)messageIds.count);
- });
- }
- - (void)clearAllMessageStatus {
- dispatch_async(self.ioQueue, ^{
- [self.statusCache removeAllObjects];
-
- // 删除文件
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if ([fileManager fileExistsAtPath:self.plistPath]) {
- NSError *error = nil;
- [fileManager removeItemAtPath:self.plistPath error:&error];
- if (error) {
- NSLog(@"[MessageStatusManager] 清空失败: %@", error);
- } else {
- NSLog(@"[MessageStatusManager] 清空成功");
- }
- }
- });
- }
- - (NSInteger)getAllMessageStatusCount {
- __block NSInteger count = 0;
- dispatch_sync(self.ioQueue, ^{
- count = self.statusCache.count;
- });
- return count;
- }
- #pragma mark - Private Methods
- /// 获取 plist 文件路径
- - (NSString *)plistFilePath {
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths firstObject];
- return [documentsDirectory stringByAppendingPathComponent:kMessageStatusPlistName];
- }
- /// 从磁盘加载缓存
- - (void)loadCache {
- dispatch_sync(self.ioQueue, ^{
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if ([fileManager fileExistsAtPath:self.plistPath]) {
- NSDictionary *diskData = [NSDictionary dictionaryWithContentsOfFile:self.plistPath];
- if (diskData) {
- self.statusCache = [diskData mutableCopy];
- NSLog(@"[MessageStatusManager] 加载缓存成功: %lu 条", (unsigned long)self.statusCache.count);
- } else {
- self.statusCache = [NSMutableDictionary dictionary];
- NSLog(@"[MessageStatusManager] 缓存文件为空,创建新缓存");
- }
- } else {
- self.statusCache = [NSMutableDictionary dictionary];
- NSLog(@"[MessageStatusManager] 缓存文件不存在,创建新缓存");
- }
- });
- }
- /// 将缓存保存到磁盘
- - (void)saveCacheToDisk {
- // 注意: 此方法必须在 ioQueue 中调用
- BOOL success = [self.statusCache writeToFile:self.plistPath atomically:YES];
- if (!success) {
- NSLog(@"[MessageStatusManager] 保存到磁盘失败");
- }
- }
- @end
|