Skip to content

Commit 4f473c7

Browse files
author
lijianghuai
committed
增加 一些单例的创建回调,方便外部改属性
1 parent fd40b7a commit 4f473c7

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

LKDBHelper.podspec.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "LKDBHelper",
3-
"version": "2.5.3",
3+
"version": "2.5.5",
44
"summary": "全自动的插入,查询,更新,删除, an automatic database operation thread-safe and not afraid of recursive deadlock",
55
"description": "全面支持 NSArray,NSDictionary, ModelClass, NSNumber, NSString, NSDate, NSData, UIColor, UIImage, CGRect, CGPoint, CGSize, NSRange, int,char,float, double, long.. 等属性的自动化操作(插入和查询)",
66
"homepage": "https://github.com/li6185377/LKDBHelper-SQLite-ORM",
@@ -10,7 +10,7 @@
1010
},
1111
"source": {
1212
"git": "https://github.com/li6185377/LKDBHelper-SQLite-ORM.git",
13-
"tag": "2.5.3"
13+
"tag": "2.5.5"
1414
},
1515
"platforms": {
1616
"ios": "5.0",

LKDBHelper/Helper/LKDBUtils.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ NS_ASSUME_NONNULL_BEGIN
4343

4444
@end
4545

46+
/// 需要外部实现对应 API
47+
@interface LKDBUtils (output)
48+
49+
// LKDateFormatter 创建完毕, 可以修改对应属性
50+
+ (void)onCreateWithDateFormatter:(NSDateFormatter *)dateFormatter;
51+
52+
// NSNumberFormatter 创建完毕, 可以修改对应属性
53+
+ (void)onCreateWithNumberFormatter:(NSNumberFormatter *)numberFormatter;
54+
55+
@end
56+
4657
#ifdef DEBUG
4758
#ifdef NSLog
4859
#define LKErrorLog(fmt, ...) NSLog(@"#LKDBHelper ERROR:\n" fmt, ##__VA_ARGS__);

LKDBHelper/Helper/LKDBUtils.m

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
#import "LKDBUtils.h"
1010

1111
@interface LKDateFormatter : NSDateFormatter
12-
@property (nonatomic, strong) NSRecursiveLock *lock;
12+
@property (nonatomic, assign) dispatch_semaphore_t lock;
1313
@end
1414

1515
@implementation LKDateFormatter
16-
- (id)init {
16+
17+
- (instancetype)init {
1718
self = [super init];
1819
if (self) {
19-
self.lock = [[NSRecursiveLock alloc] init];
20+
self.lock = dispatch_semaphore_create(1);
2021
self.generatesCalendarDates = YES;
2122
self.dateStyle = NSDateFormatterNoStyle;
2223
self.timeStyle = NSDateFormatterNoStyle;
@@ -26,43 +27,62 @@ - (id)init {
2627
if (locale) {
2728
[self setLocale:locale];
2829
}
30+
if ([LKDBUtils respondsToSelector:@selector(onCreateWithDateFormatter:)]) {
31+
[LKDBUtils onCreateWithDateFormatter:self];
32+
}
2933
}
3034
return self;
3135
}
36+
3237
//防止 iOS5 多线程 格式化时间时 崩溃
3338
- (NSDate *)dateFromString:(NSString *)string {
34-
[_lock lock];
39+
dispatch_semaphore_wait(_lock, DISPATCH_TIME_FOREVER);
3540
NSDate *date = [super dateFromString:string];
36-
[_lock unlock];
41+
dispatch_semaphore_signal(_lock);
3742
return date;
3843
}
44+
3945
- (NSString *)stringFromDate:(NSDate *)date {
40-
[_lock lock];
46+
dispatch_semaphore_wait(_lock, DISPATCH_TIME_FOREVER);
4147
NSString *string = [super stringFromDate:date];
42-
[_lock unlock];
48+
dispatch_semaphore_signal(_lock);
4349
return string;
4450
}
51+
4552
@end
4653

4754
@interface LKNumberFormatter : NSNumberFormatter
4855

4956
@end
5057

5158
@implementation LKNumberFormatter
59+
60+
- (instancetype)init {
61+
self = [super init];
62+
if (self) {
63+
if ([LKDBUtils respondsToSelector:@selector(onCreateWithNumberFormatter:)]) {
64+
[LKDBUtils onCreateWithNumberFormatter:self];
65+
}
66+
}
67+
return self;
68+
}
69+
5270
- (NSString *)stringFromNumber:(NSNumber *)number {
5371
NSString *string = [number stringValue];
5472
if (!string) {
5573
string = [NSString stringWithFormat:@"%lf", [number doubleValue]];
5674
}
5775
return string;
5876
}
77+
5978
- (NSNumber *)numberFromString:(NSString *)string {
6079
NSNumber *number = [super numberFromString:string];
6180
if (!number) {
6281
number = @(string.doubleValue);
6382
}
6483
return number;
6584
}
85+
6686
@end
6787

6888
@implementation LKDBUtils
@@ -125,6 +145,7 @@ + (NSString *)getDocumentPath {
125145
return homePath;
126146
#endif
127147
}
148+
128149
+ (NSString *)getDirectoryForDocuments:(NSString *)dir {
129150
NSString *dirPath = [[self getDocumentPath] stringByAppendingPathComponent:dir];
130151
BOOL isDir = NO;
@@ -137,23 +158,29 @@ + (NSString *)getDirectoryForDocuments:(NSString *)dir {
137158
}
138159
return dirPath;
139160
}
161+
140162
+ (NSString *)getPathForDocuments:(NSString *)filename {
141163
return [[self getDocumentPath] stringByAppendingPathComponent:filename];
142164
}
165+
143166
+ (NSString *)getPathForDocuments:(NSString *)filename inDir:(NSString *)dir {
144167
return [[self getDirectoryForDocuments:dir] stringByAppendingPathComponent:filename];
145168
}
169+
146170
+ (BOOL)isFileExists:(NSString *)filepath {
147171
return [[NSFileManager defaultManager] fileExistsAtPath:filepath];
148172
}
173+
149174
+ (BOOL)deleteWithFilepath:(NSString *)filepath {
150175
return [[NSFileManager defaultManager] removeItemAtPath:filepath error:nil];
151176
}
177+
152178
+ (NSArray *)getFilenamesWithDir:(NSString *)dir {
153179
NSFileManager *fileManager = [NSFileManager defaultManager];
154180
NSArray *fileList = [fileManager contentsOfDirectoryAtPath:dir error:nil];
155181
return fileList;
156182
}
183+
157184
+ (BOOL)checkStringIsEmpty:(NSString *)string {
158185
if (string == nil) {
159186
return YES;
@@ -179,6 +206,7 @@ + (NSDateFormatter *)getDBDateFormat {
179206
});
180207
return format;
181208
}
209+
182210
+ (NSString *)stringWithDate:(NSDate *)date {
183211
NSDateFormatter *formatter = [self getDBDateFormat];
184212
NSString *datestr = [formatter stringFromDate:date];
@@ -187,11 +215,13 @@ + (NSString *)stringWithDate:(NSDate *)date {
187215
}
188216
return datestr;
189217
}
218+
190219
+ (NSDate *)dateWithString:(NSString *)str {
191220
NSDateFormatter *formatter = [self getDBDateFormat];
192221
NSDate *date = [formatter dateFromString:str];
193222
return date;
194223
}
224+
195225
+ (NSNumberFormatter *)numberFormatter {
196226
static NSNumberFormatter *numberFormatter = nil;
197227
static dispatch_once_t onceToken;
@@ -200,6 +230,7 @@ + (NSNumberFormatter *)numberFormatter {
200230
});
201231
return numberFormatter;
202232
}
233+
203234
@end
204235

205236
inline NSString *LKSQLTypeFromObjcType(NSString *objcType) {

0 commit comments

Comments
 (0)