iOS18 第三库YYCache中sqlite3_finalize(stmt)调用报错修复

YYCache的YYKVStorage类中的有sqlite3_finalize(stmt)调用时报错一劳永逸无损修复

.h

objectivec 复制代码
#import "YYKVStorage.h"

NS_ASSUME_NONNULL_BEGIN
@interface YYKVStorage (Fix)
@property (nonatomic, readonly) NSString *path;        ///< The path of this storage.
@property (nonatomic, readonly) YYKVStorageType type;  ///< The type of this storage.
@property (nonatomic) BOOL errorLogsEnabled;           ///< Set `YES` to enable error logs for debug.
@end

NS_ASSUME_NONNULL_END

.m

objectivec 复制代码
#import "YYKVStorage+Fix.h"
#import <UIKit/UIKit.h>
#import <time.h>

#if __has_include(<sqlite3.h>)
#import <sqlite3.h>
#else
#import "sqlite3.h"
#endif
@interface YYKVStorage ()
{
    dispatch_queue_t _trashQueue;
    
    NSString *_path;
    NSString *_dbPath;
    NSString *_dataPath;
    NSString *_trashPath;
    
    sqlite3 *_db;
    CFMutableDictionaryRef _dbStmtCache;
    NSTimeInterval _dbLastOpenErrorTime;
    NSUInteger _dbOpenErrorCount;
}
@end

@implementation YYKVStorage (Fix)

#pragma mark - db

- (BOOL)_dbOpen {
    if (_db) return YES;
    
    int result = sqlite3_open(_dbPath.UTF8String, &_db);
    if (result == SQLITE_OK) {
        CFDictionaryKeyCallBacks keyCallbacks = kCFCopyStringDictionaryKeyCallBacks;
        CFDictionaryValueCallBacks valueCallbacks = {0};
        _dbStmtCache = CFDictionaryCreateMutable(CFAllocatorGetDefault(), 0, &keyCallbacks, &valueCallbacks);
        _dbLastOpenErrorTime = 0;
        _dbOpenErrorCount = 0;
        return YES;
    } else {
        _db = NULL;
            if (_dbStmtCache) {
        CFIndex size = CFDictionaryGetCount(_dbStmtCache);
        CFTypeRef *valuesRef = (CFTypeRef *)malloc(size * sizeof(CFTypeRef));
        CFDictionaryGetKeysAndValues(_dbStmtCache, NULL, (const void **)valuesRef);
        const sqlite3_stmt **stmts = (const sqlite3_stmt **)valuesRef;
        for (CFIndex i = 0; i < size; i ++) {
            sqlite3_stmt *stmt = stmts[i];
            sqlite3_finalize(stmt);
        }
        free(valuesRef);
        CFRelease(_dbStmtCache);
    }
        _dbStmtCache = NULL;
        _dbLastOpenErrorTime = CACurrentMediaTime();
        _dbOpenErrorCount++;
        
//        if (_errorLogsEnabled) {
//            NSLog(@"%s line:%d sqlite open failed (%d).", __FUNCTION__, __LINE__, result);
//        }
        return NO;
    }
}

- (BOOL)_dbClose {
    if (!_db) return YES;
    
    int  result = 0;
    BOOL retry = NO;
    BOOL stmtFinalized = NO;
    
        if (_dbStmtCache) {
        CFIndex size = CFDictionaryGetCount(_dbStmtCache);
        CFTypeRef *valuesRef = (CFTypeRef *)malloc(size * sizeof(CFTypeRef));
        CFDictionaryGetKeysAndValues(_dbStmtCache, NULL, (const void **)valuesRef);
        const sqlite3_stmt **stmts = (const sqlite3_stmt **)valuesRef;
        for (CFIndex i = 0; i < size; i ++) {
            sqlite3_stmt *stmt = stmts[i];
            sqlite3_finalize(stmt);
        }
        free(valuesRef);
        CFRelease(_dbStmtCache);
    }
    _dbStmtCache = NULL;
    
    do {
        retry = NO;
        result = sqlite3_close(_db);
        if (result == SQLITE_BUSY || result == SQLITE_LOCKED) {
            if (!stmtFinalized) {
                stmtFinalized = YES;
                sqlite3_stmt *stmt;
                while ((stmt = sqlite3_next_stmt(_db, nil)) != 0) {
                    sqlite3_finalize(stmt);
                    retry = YES;
                }
            }
        } else if (result != SQLITE_OK) {
//            if (_errorLogsEnabled) {
//                NSLog(@"%s line:%d sqlite close failed (%d).", __FUNCTION__, __LINE__, result);
//            }
        }
    } while (retry);
    _db = NULL;
    return YES;
}

@end
相关推荐
ii_best1 小时前
按键精灵ios脚本新增元素功能助力辅助工具开发(二)
ios
ii_best1 小时前
按键精灵ios脚本新增元素功能助力辅助工具开发(一)
ios
可喜~可乐4 小时前
C# SQLite高级功能示例
数据库·sql·sqlite·c#
Swift社区6 小时前
Swift实战:如何优雅地从二叉搜索树中挑出最接近的K个值
开发语言·ios·swift
A~taoker6 小时前
django扩展练习记录
数据库·django·sqlite
努力的搬砖人.7 小时前
SQLite 转换为 MySQL 数据库
数据库·mysql·sqlite
I烟雨云渊T10 小时前
iOS即时通信的技术要点
ios
鸿蒙布道师13 小时前
鸿蒙NEXT开发动画案例5
android·ios·华为·harmonyos·鸿蒙系统·arkui·huawei
WDeLiang19 小时前
Flutter - UIKit开发相关指南 - 导航
flutter·ios·dart
文件夹__iOS21 小时前
深入浅出 iOS 对象模型:isa 指针 与 Swift Metadata
ios·swift