Explorar el Código

程序闪退情况下数据本地存储

Pen Li hace 9 años
padre
commit
d1717e8bde

BIN
RedAnt ERP Mobile/RedAnt ERP Mobile.xcworkspace/xcuserdata/macmini1.xcuserdatad/UserInterfaceState.xcuserstate


+ 4 - 0
RedAnt ERP Mobile/common/CommonEditor/CommonEditorViewController.h

@@ -10,6 +10,7 @@
 #import "SignatureViewController.h"
 #import "EnumSelectViewController.h"
 #import "MonthPickerViewController.h"
+#import "JKTimerManager.h"
 
 @interface subitem_data : NSObject
 @property (strong, nonatomic) NSMutableDictionary *params;
@@ -75,4 +76,7 @@
 
 //control cell event;
 
+- (void)start_urgency_timer;
+- (void)cancel_urgency_timer;
+
 @end

+ 85 - 1
RedAnt ERP Mobile/common/CommonEditor/CommonEditorViewController.m

@@ -35,7 +35,7 @@
 #define INTNUMBERS @"0123456789\n"
 #define NUMBERS @"0123456789.\n"
 #import "OLDataProvider.h"
-
+#import <objc/objc.h>
 
 
 @interface subitem_data ()
@@ -46,10 +46,30 @@
 @end
 @interface CommonEditorViewController ()
 
+
+@property (nonatomic,copy) NSString *class_name;
+
+@property (nonatomic,copy) NSString *urgency_timer_name;
+
 @end
 
 @implementation CommonEditorViewController
 
+- (NSString *)class_name {
+    if (!_class_name) {
+        const char *class_name_ch = object_getClassName([self class]);
+        _class_name = [NSString stringWithUTF8String:class_name_ch];
+    }
+    return _class_name;
+}
+
+- (NSString *)urgency_timer_name {
+    if (!_urgency_timer_name) {
+        _urgency_timer_name = [NSString stringWithFormat:@"%@_urgency_timer",self.class_name];
+    }
+    return _urgency_timer_name;
+}
+
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
@@ -74,6 +94,7 @@
 -(void) viewWillDisappear:(BOOL)animated
 {
     [[NSNotificationCenter defaultCenter] removeObserver:self];
+    [self cancel_urgency_timer];
 }
 
 -(void)manually_refresh
@@ -98,10 +119,73 @@
     reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
     [self refresh:nil];
 }
+
+- (void)start_urgency_timer {
+    
+    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
+    
+    
+    [[JKTimerManager sharedTimerManager] scheduledDispatchTimerWithName:self.urgency_timer_name timeInterval:2 queue:nil repeats:YES action:^{
+        
+        [appDelegate.urgencyDic setValue:appDelegate.user forKey:@"user"];
+        [appDelegate.urgencyDic setValue:[NSNumber numberWithBool:appDelegate.offline_mode] forKey:@"offline_mode"];
+        
+        if ([self.class_name isEqualToString:@"CustomerEditViewController"]) {
+            // CustomerEditViewController
+            __block NSString *contact_id = @"";
+            
+            [self.content_data_download enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
+                
+                // section_0
+                if ([key isEqualToString:@"section_0"]) {
+                    NSDictionary *section_0 = (NSDictionary *)obj;
+                    [section_0 enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
+                    
+                        // Contact ID
+                        if ([obj isKindOfClass:[NSDictionary class]]) {
+                            if ([[obj objectForKey:@"aname"] isEqualToString:@"Contact ID"]) {
+                                contact_id = [obj objectForKey:@"value"];
+                            }
+                            
+                        }
+                        
+                    }];
+                }
+
+            }];
+            [appDelegate.urgencyDic setValue:self.class_name forKey:@"firt_class_name"];
+            [appDelegate.urgencyDic setValue:contact_id forKey:@"contact_id"];
+            
+            
+        } else if ([self.class_name isEqualToString:@"CreateOrderViewController"]) {
+            // CreateOrderViewController
+            [appDelegate.urgencyDic setValue:self.class_name forKey:@"firt_class_name"];
+            
+            
+        } else if ([self.class_name isEqualToString:@"AddressEditorViewController"]) {
+            // AddressEditorViewController
+            
+            [appDelegate.urgencyDic setValue:self.class_name forKey:@"second_class_name"];
+        }
+        
+        
+        [appDelegate.urgencyDic setValue:self.changed_data forKey:[NSString stringWithFormat:@"%@_changed_data",self.class_name]];
+        
+        
+    }];
+}
+
+- (void)cancel_urgency_timer {
+    [[JKTimerManager sharedTimerManager] cancelTimerWithName:self.urgency_timer_name];
+}
+
 - (void)viewDidLoad
 {
     [super viewDidLoad];
     
+    [self start_urgency_timer];
+    
+    
     if(self.loading_msg==nil)
         self.loading_msg = @"Please Wait";
     if(self.loading_title==nil)

+ 14 - 0
RedAnt ERP Mobile/iSales-NPD.xcodeproj/project.pbxproj

@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		421C55021D81586D00CFA3B1 /* JKTimerManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 421C55011D81586D00CFA3B1 /* JKTimerManager.m */; };
 		423A4ADC1D503A53005ECE4A /* createContact.json in Resources */ = {isa = PBXBuildFile; fileRef = 423A4ADB1D503A53005ECE4A /* createContact.json */; };
 		42969C021D52F31C00FF190A /* editContact.json in Resources */ = {isa = PBXBuildFile; fileRef = 42969C011D52F31C00FF190A /* editContact.json */; };
 		42A225331D6E7D1D00235B62 /* placeOrderTemplate.json in Resources */ = {isa = PBXBuildFile; fileRef = 42A225321D6E7D1D00235B62 /* placeOrderTemplate.json */; };
@@ -204,6 +205,8 @@
 /* End PBXBuildFile section */
 
 /* Begin PBXFileReference section */
+		421C55001D81586D00CFA3B1 /* JKTimerManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JKTimerManager.h; sourceTree = "<group>"; };
+		421C55011D81586D00CFA3B1 /* JKTimerManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JKTimerManager.m; sourceTree = "<group>"; };
 		423A4ADB1D503A53005ECE4A /* createContact.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = createContact.json; sourceTree = "<group>"; };
 		42969C011D52F31C00FF190A /* editContact.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = editContact.json; sourceTree = "<group>"; };
 		42A225321D6E7D1D00235B62 /* placeOrderTemplate.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = placeOrderTemplate.json; sourceTree = "<group>"; };
@@ -588,6 +591,15 @@
 /* End PBXFrameworksBuildPhase section */
 
 /* Begin PBXGroup section */
+		421C54FF1D81586D00CFA3B1 /* JKTimer */ = {
+			isa = PBXGroup;
+			children = (
+				421C55001D81586D00CFA3B1 /* JKTimerManager.h */,
+				421C55011D81586D00CFA3B1 /* JKTimerManager.m */,
+			);
+			path = JKTimer;
+			sourceTree = "<group>";
+		};
 		42E58BFF1D7E735A0092810A /* UIColor+HEX */ = {
 			isa = PBXGroup;
 			children = (
@@ -829,6 +841,7 @@
 		716387C71953CDB4006E65E6 /* utils */ = {
 			isa = PBXGroup;
 			children = (
+				421C54FF1D81586D00CFA3B1 /* JKTimer */,
 				42E58BFF1D7E735A0092810A /* UIColor+HEX */,
 				71BBA2171CEAC10200C91DED /* zip */,
 				71B1250B1C55BD4600118904 /* QRCODE */,
@@ -1445,6 +1458,7 @@
 				42E58BFB1D7E5EF50092810A /* SortButton.m in Sources */,
 				710274251CC606C4009FD219 /* UserListViewController.m in Sources */,
 				71DF74881C57608F00F2789C /* PulldownMenu.m in Sources */,
+				421C55021D81586D00CFA3B1 /* JKTimerManager.m in Sources */,
 				7141DD551C57459B00F7DF59 /* split.c in Sources */,
 				719562421CF5828200C74A49 /* DefaultTableHeaderView.m in Sources */,
 				716AF8E11D7AA0E0001188E0 /* SelectUploadOrderViewController.m in Sources */,

+ 1 - 1
RedAnt ERP Mobile/iSales-NPD/AppDelegate.h

@@ -132,7 +132,7 @@
 -(void) printPdf:(NSString*) url company:(NSString*)company send_to:(NSString*)send_to soid:(NSString*)soid content:(NSString*)content;
 @property (strong, nonatomic) NSMutableDictionary* OrderFilter;
 
-
+@property (nonatomic,strong) NSMutableDictionary *urgencyDic;///<程序闪退需要保存的数据
 
 
 @end

+ 16 - 1
RedAnt ERP Mobile/iSales-NPD/AppDelegate.m

@@ -520,10 +520,25 @@ void UncaughtExceptionHandler(NSException *exception) {
     
 }
 
+- (void)initialExceptionHandler {
+    NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
+    
+    // 检查本地urgency文件,存在则加载进_urgencyDic
+    NSFileManager *manager = [NSFileManager defaultManager];
+    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
+    NSString *urgencyFile = [cachePath stringByAppendingPathComponent:@"urgency"];
+    
+    if ([manager fileExistsAtPath:urgencyFile]) {
+        _urgencyDic = [NSMutableDictionary dictionaryWithContentsOfFile:urgencyFile];
+    } else {
+        _urgencyDic = [NSMutableDictionary dictionary];
+    }
+    
+}
 
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
-    NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
+    [self initialExceptionHandler];
     
     [DefaultAppearance init_appearance];
     

+ 38 - 0
RedAnt ERP Mobile/iSales-NPD/JKTimer/JKTimerManager.h

@@ -0,0 +1,38 @@
+//
+//  JKTimer.h
+//  JKUtilDemo
+//
+//  Created by Jack on 7/1/16.
+//  Copyright © 2016 Emerys. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface JKTimerManager : NSObject
+
++ (instancetype)sharedTimerManager;
+
+/**
+ *  启动一个timer,默认精度为0.1秒
+ *
+ *  @param name          timer的名称,作为唯一标识
+ *  @param timerInterval 执行的时间间隔
+ *  @param queue         timer将被放入的队列,也就是最终action执行的队列。传入nil将自动放到一个子线程队列中
+ *  @param repeats       timer是否循环调用
+ *  @param action        时间间隔到点时执行的block
+ */
+- (void)scheduledDispatchTimerWithName:(NSString *)name timeInterval:(NSTimeInterval)timerInterval queue:(dispatch_queue_t)queue repeats:(BOOL)repeats action:(dispatch_block_t)action;
+
+/**
+ *  撤销某个timer
+ *
+ *  @param name timer的名称,唯一标识
+ */
+- (void)cancelTimerWithName:(NSString *)name;
+
+/**
+ *  撤销所有timer 
+ */
+- (void)cancelAllTimer;
+
+@end

+ 82 - 0
RedAnt ERP Mobile/iSales-NPD/JKTimer/JKTimerManager.m

@@ -0,0 +1,82 @@
+//
+//  JKTimer.m
+//  JKUtilDemo
+//
+//  Created by Jack on 7/1/16.
+//  Copyright © 2016 Emerys. All rights reserved.
+//
+
+#import "JKTimerManager.h"
+
+@interface JKTimerManager ()
+
+@property (nonatomic,strong) NSMutableDictionary *timerContainer;
+
+@end
+
+@implementation JKTimerManager
+
++ (instancetype)sharedTimerManager {
+    static JKTimerManager *manager = nil;
+    static dispatch_once_t token;
+    dispatch_once(&token, ^{
+        manager = [[JKTimerManager alloc] init];
+    });
+    
+    return manager;
+}
+
+- (NSMutableDictionary *)timerContainer {
+    if (!_timerContainer) {
+        _timerContainer = [NSMutableDictionary dictionary];
+    }
+    return _timerContainer;
+}
+
+- (void)scheduledDispatchTimerWithName:(NSString *)name
+                          timeInterval:(NSTimeInterval)timerInterval
+                                 queue:(dispatch_queue_t)queue
+                               repeats:(BOOL)repeats
+                                action:(dispatch_block_t)action {
+    if (name == nil)
+        return;
+    if (queue == nil)
+        queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
+    
+    dispatch_source_t timer = [self.timerContainer objectForKey:name];
+    if (!timer) {
+        timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
+        dispatch_resume(timer);
+        [self.timerContainer setObject:timer forKey:name];
+    }
+    
+    dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, timerInterval * NSEC_PER_SEC), timerInterval * NSEC_PER_SEC, 0.1 * NSEC_PER_SEC);
+    __weak typeof(self) weakSelf = self;
+    dispatch_source_set_event_handler(timer, ^{
+        if (action) {
+            action();
+            if (!repeats) {
+                [weakSelf cancelTimerWithName:name];
+            }
+        }
+    });
+    
+}
+
+- (void)cancelTimerWithName:(NSString *)name {
+    dispatch_source_t timer = [self.timerContainer objectForKey:name];
+    if (!timer) {
+        return;
+    }
+    
+    [self.timerContainer removeObjectForKey:name];
+    dispatch_source_cancel(timer);
+}
+
+- (void)cancelAllTimer {
+    for (NSString *name in self.timerContainer.allKeys) {
+        [self cancelTimerWithName:name];
+    }
+}
+
+@end