本地数据库


注册异常处理Handler

/**
 *  数据库异常信息
 */
@interface NIMDatabaseException : NSObject

/**
 * 异常
 */
@property (nonatomic,assign,readonly) NIMDatabaseExceptionType exception;

/**
 *  数据库异常信息
 */
@property (nullable,nonatomic,copy,readonly) NSString * message;

/**
 *  数据库文件沙盒路径
 */
@property (nullable,nonatomic,copy,readonly) NSString * databasePath;

/**
 *  注册数据库异常处理对象
 *  @param handler 用户自定义处理对象
 */
+ (void)registerExceptionHandler:(id<NIMDatabaseHandleExceptionProtocol>)handler;

@end

实例代码

NTESDbExceptionHandler * handler = [[NTESDbExceptionHandler alloc] init];
[NIMDatabaseException registerExceptionHandler:handler];
// NTESDbExceptionHandler.mm

@implementation NTESDbExceptionHandler

- (void)handleException:(NIMDatabaseException *)exception
{
    self.count ++;

    NSString * codeType = nil;
    switch (exception.exception) {

        case NIMDatabaseExceptionTypeBadDb:
            codeType = @"数据库损坏";
            break;
        default:
            break;
        }


    NSString * msg = [NSString stringWithFormat:@"总次数%zu\n错误:%@\n信息:%@\n路径:%@\n",self.count, codeType,exception.message, exception.databasePath];
    NSLog(@"handleException: %@",msg);
    [[UIApplication sharedApplication].keyWindow hideToasts];
    [[UIApplication sharedApplication].keyWindow makeToast:msg
                                                  duration:3
                                                  position:CSToastPositionCenter];
}

@end