Authored by 王钱钧

Merge remote-tracking branch 'origin/yoho' into yoho

@@ -232,11 +232,13 @@ static dispatch_queue_t persisting_queue() { @@ -232,11 +232,13 @@ static dispatch_queue_t persisting_queue() {
232 self.device.ak = appId; 232 self.device.ak = appId;
233 self.device.ch = channelId; 233 self.device.ch = channelId;
234 234
235 - // 设置device  
236 - [self.immediUploadItemDic setObject:self.device.jsonDictionary forKey:JsonKeyDataTypeDevice];  
237 -  
238 - // 设置status  
239 - [self.immediUploadItemDic setObject: [[NSMutableArray alloc]initWithObjects:self.currentStatus.jsonDictionary, nil] forKey:JsonKeyDataTypeStatus]; 235 + @synchronized(self.immediUploadItemDic) {
  236 + // 设置device
  237 + [self.immediUploadItemDic setObject:self.device.jsonDictionary forKey:JsonKeyDataTypeDevice];
  238 +
  239 + // 设置status
  240 + [self.immediUploadItemDic setObject: [[NSMutableArray alloc]initWithObjects:self.currentStatus.jsonDictionary, nil] forKey:JsonKeyDataTypeStatus];
  241 + }
240 242
241 [self startObserverNetworkReachabilityStatus]; 243 [self startObserverNetworkReachabilityStatus];
242 244
@@ -369,48 +371,54 @@ static dispatch_queue_t persisting_queue() { @@ -369,48 +371,54 @@ static dispatch_queue_t persisting_queue() {
369 if (itemData.dataType == YHItemDataTypeEvent) { 371 if (itemData.dataType == YHItemDataTypeEvent) {
370 YHEvent *event = (YHEvent *)itemData; 372 YHEvent *event = (YHEvent *)itemData;
371 if (event) { 373 if (event) {
372 - [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypePerfor];  
373 - [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypeErrors];  
374 - [self.immediUploadItemDic setObject:[[NSMutableArray alloc]initWithObjects:event.jsonDictionary, nil] forKey:JsonKeyDataTypeEvents];  
375 -  
376 - [[YHNetworkService sharedInstance] uploadLogData:self.immediUploadItemDic completionBlock:^(BOOL success, NSError *error) {  
377 - if (success) {  
378 - YALog(@"upload immedialite event success.");  
379 - } else {  
380 - YALog(@"uploadImmedilyWithEvent error = %@", error.localizedDescription);  
381 - }  
382 - }]; 374 + @synchronized(self.immediUploadItemDic) {
  375 + [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypePerfor];
  376 + [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypeErrors];
  377 + [self.immediUploadItemDic setObject:[[NSMutableArray alloc]initWithObjects:event.jsonDictionary, nil] forKey:JsonKeyDataTypeEvents];
  378 +
  379 + [[YHNetworkService sharedInstance] uploadLogData:self.immediUploadItemDic completionBlock:^(BOOL success, NSError *error) {
  380 + if (success) {
  381 + YALog(@"upload immedialite event success.");
  382 + } else {
  383 + YALog(@"uploadImmedilyWithEvent error = %@", error.localizedDescription);
  384 + }
  385 + }];
  386 + }
383 } 387 }
384 } else if (itemData.dataType == YHItemDataTypePerformance) { 388 } else if (itemData.dataType == YHItemDataTypePerformance) {
385 YHPerformance *perfms = (YHPerformance *)itemData; 389 YHPerformance *perfms = (YHPerformance *)itemData;
386 perfms.net = self.currentStatus.net; 390 perfms.net = self.currentStatus.net;
387 if (perfms) { 391 if (perfms) {
388 - [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypeEvents];  
389 - [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypeErrors];  
390 - [self.immediUploadItemDic setObject:[[NSMutableArray alloc]initWithObjects:perfms.jsonDictionary, nil] forKey:JsonKeyDataTypePerfor];  
391 -  
392 - [[YHNetworkService sharedInstance] uploadLogData:self.immediUploadItemDic completionBlock:^(BOOL success, NSError *error) {  
393 - if (success) {  
394 - YALog(@"upload immedialite event success.");  
395 - } else {  
396 - YALog(@"uploadImmedilyWithEvent error = %@", error.localizedDescription);  
397 - }  
398 - }]; 392 + @synchronized(self.immediUploadItemDic) {
  393 + [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypeEvents];
  394 + [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypeErrors];
  395 + [self.immediUploadItemDic setObject:[[NSMutableArray alloc]initWithObjects:perfms.jsonDictionary, nil] forKey:JsonKeyDataTypePerfor];
  396 +
  397 + [[YHNetworkService sharedInstance] uploadLogData:self.immediUploadItemDic completionBlock:^(BOOL success, NSError *error) {
  398 + if (success) {
  399 + YALog(@"upload immedialite event success.");
  400 + } else {
  401 + YALog(@"uploadImmedilyWithEvent error = %@", error.localizedDescription);
  402 + }
  403 + }];
  404 + }
399 } 405 }
400 } else if(itemData.dataType == YHItemDataTypeError) { 406 } else if(itemData.dataType == YHItemDataTypeError) {
401 YHError *error = (YHError *)itemData; 407 YHError *error = (YHError *)itemData;
402 if (error) { 408 if (error) {
403 - [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypeEvents];  
404 - [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypePerfor];  
405 - [self.immediUploadItemDic setObject:[[NSMutableArray alloc]initWithObjects:error.jsonDictionary, nil] forKey:JsonKeyDataTypeErrors];  
406 -  
407 - [[YHNetworkService sharedInstance] uploadLogData:self.immediUploadItemDic completionBlock:^(BOOL success, NSError *error) {  
408 - if (success) {  
409 - YALog(@"upload immedialite error success.");  
410 - } else {  
411 - YALog(@"uploadImmedilyWithError error = %@", error.localizedDescription);  
412 - }  
413 - }]; 409 + @synchronized(self.immediUploadItemDic) {
  410 + [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypeEvents];
  411 + [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypePerfor];
  412 + [self.immediUploadItemDic setObject:[[NSMutableArray alloc]initWithObjects:error.jsonDictionary, nil] forKey:JsonKeyDataTypeErrors];
  413 +
  414 + [[YHNetworkService sharedInstance] uploadLogData:self.immediUploadItemDic completionBlock:^(BOOL success, NSError *error) {
  415 + if (success) {
  416 + YALog(@"upload immedialite error success.");
  417 + } else {
  418 + YALog(@"uploadImmedilyWithError error = %@", error.localizedDescription);
  419 + }
  420 + }];
  421 + }
414 } 422 }
415 } else { 423 } else {
416 // 不处理非 Event 和 Error 类型的数据 424 // 不处理非 Event 和 Error 类型的数据
@@ -568,15 +576,17 @@ static dispatch_queue_t persisting_queue() { @@ -568,15 +576,17 @@ static dispatch_queue_t persisting_queue() {
568 // 更新立即上传信息 576 // 更新立即上传信息
569 - (void)updateImmediUploadDic 577 - (void)updateImmediUploadDic
570 { 578 {
571 - NSMutableArray *allStatus = [self.immediUploadItemDic objectForKey:JsonKeyDataTypeStatus];  
572 - if ([allStatus count]) {  
573 - [allStatus replaceObjectAtIndex:0 withObject:self.currentStatus.jsonDictionary];  
574 - [self.immediUploadItemDic setObject:allStatus forKey:JsonKeyDataTypeStatus];  
575 - } else {  
576 - NSException *exception = [NSException exceptionWithName:NSGenericException  
577 - reason:@"appId and sessionId do not be initialized. Please use prepareImmediUploadDic:sessionId:channelId: first."  
578 - userInfo:nil];  
579 - [exception raise]; 579 + @synchronized(self.immediUploadItemDic) {
  580 + NSMutableArray *allStatus = [self.immediUploadItemDic objectForKey:JsonKeyDataTypeStatus];
  581 + if ([allStatus count]) {
  582 + [allStatus replaceObjectAtIndex:0 withObject:self.currentStatus.jsonDictionary];
  583 + [self.immediUploadItemDic setObject:allStatus forKey:JsonKeyDataTypeStatus];
  584 + } else {
  585 + NSException *exception = [NSException exceptionWithName:NSGenericException
  586 + reason:@"appId and sessionId do not be initialized. Please use prepareImmediUploadDic:sessionId:channelId: first."
  587 + userInfo:nil];
  588 + [exception raise];
  589 + }
580 } 590 }
581 } 591 }
582 592
@@ -606,7 +616,9 @@ static dispatch_queue_t persisting_queue() { @@ -606,7 +616,9 @@ static dispatch_queue_t persisting_queue() {
606 collectData = [[NSMutableDictionary alloc]init]; 616 collectData = [[NSMutableDictionary alloc]init];
607 617
608 //直接添加 immediUploadDataDic 内容(里面包含 device 信息) 618 //直接添加 immediUploadDataDic 内容(里面包含 device 信息)
609 - [collectData addEntriesFromDictionary:self.immediUploadItemDic]; 619 + @synchronized(self.immediUploadItemDic) {
  620 + [collectData addEntriesFromDictionary:self.immediUploadItemDic];
  621 + }
610 622
611 //组合event信息 623 //组合event信息
612 if (event.jsonDictionary) { 624 if (event.jsonDictionary) {
@@ -675,14 +687,16 @@ static dispatch_queue_t persisting_queue() { @@ -675,14 +687,16 @@ static dispatch_queue_t persisting_queue() {
675 collectData = [[NSMutableDictionary alloc]init]; 687 collectData = [[NSMutableDictionary alloc]init];
676 688
677 //直接添加 immediUploadDataDic 内容(里面包含 device 信息) 689 //直接添加 immediUploadDataDic 内容(里面包含 device 信息)
678 - [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypeErrors];  
679 - [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypeEvents];  
680 - [collectData addEntriesFromDictionary:self.immediUploadItemDic]; 690 + @synchronized(self.immediUploadItemDic) {
  691 + [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypeErrors];
  692 + [self.immediUploadItemDic removeObjectForKey:JsonKeyDataTypeEvents];
  693 + [collectData addEntriesFromDictionary:self.immediUploadItemDic];
  694 + }
681 695
682 //组合performance信息 696 //组合performance信息
683 if (performanceData.jsonDictionary) { 697 if (performanceData.jsonDictionary) {
684 NSMutableArray *events = [[NSMutableArray alloc]initWithObjects:performanceData.jsonDictionary,nil]; 698 NSMutableArray *events = [[NSMutableArray alloc]initWithObjects:performanceData.jsonDictionary,nil];
685 - // self.allEventsCount = [events count]; 699 +
686 [collectData setObject:events forKey:JsonKeyDataTypePerfor]; 700 [collectData setObject:events forKey:JsonKeyDataTypePerfor];
687 } 701 }
688 702
@@ -708,19 +722,17 @@ static dispatch_queue_t persisting_queue() { @@ -708,19 +722,17 @@ static dispatch_queue_t persisting_queue() {
708 oldPerformance = [[NSMutableArray alloc]init]; 722 oldPerformance = [[NSMutableArray alloc]init];
709 } 723 }
710 [oldPerformance addObject:performanceData.jsonDictionary]; 724 [oldPerformance addObject:performanceData.jsonDictionary];
711 - // self.allEventsCount = [oldPerformance count]; 725 +
712 [collectData setObject:oldPerformance forKey:JsonKeyDataTypePerfor]; 726 [collectData setObject:oldPerformance forKey:JsonKeyDataTypePerfor];
713 727
714 } else { 728 } else {
715 NSMutableArray *newPerformance = [[NSMutableArray alloc]initWithObjects:performanceData.jsonDictionary, nil]; 729 NSMutableArray *newPerformance = [[NSMutableArray alloc]initWithObjects:performanceData.jsonDictionary, nil];
716 - // self.allEventsCount = [newEvents count]; 730 +
717 [collectData setObject:newPerformance forKey:JsonKeyDataTypePerfor]; 731 [collectData setObject:newPerformance forKey:JsonKeyDataTypePerfor];
718 } 732 }
719 } 733 }
720 734
721 return [collectData writeToFile:self.eventFileName atomically:YES]; 735 return [collectData writeToFile:self.eventFileName atomically:YES];
722 -  
723 -  
724 } 736 }
725 737
726 // 将一个错误写入到文件 738 // 将一个错误写入到文件
@@ -743,7 +755,9 @@ static dispatch_queue_t persisting_queue() { @@ -743,7 +755,9 @@ static dispatch_queue_t persisting_queue() {
743 if (!collectData) { 755 if (!collectData) {
744 collectData = [[NSMutableDictionary alloc]init]; 756 collectData = [[NSMutableDictionary alloc]init];
745 //直接添加 immediUploadDataDic 内容(里面包含 device 信息) 757 //直接添加 immediUploadDataDic 内容(里面包含 device 信息)
746 - [collectData addEntriesFromDictionary:self.immediUploadItemDic]; 758 + @synchronized(self.immediUploadItemDic) {
  759 + [collectData addEntriesFromDictionary:self.immediUploadItemDic];
  760 + }
747 761
748 //组合error信息 762 //组合error信息
749 if (errorData.jsonDictionary) { 763 if (errorData.jsonDictionary) {
@@ -816,178 +830,160 @@ static dispatch_queue_t persisting_queue() { @@ -816,178 +830,160 @@ static dispatch_queue_t persisting_queue() {
816 830
817 - (void)writeTempEventArray 831 - (void)writeTempEventArray
818 { 832 {
819 - if ([self.tempEventArray count] > 0) {  
820 - NSMutableDictionary *collectData = [[NSMutableDictionary alloc] initWithContentsOfFile:self.eventFileName];  
821 -  
822 - //无本地持久化数据  
823 - if (!collectData) {  
824 - collectData = [[NSMutableDictionary alloc]init];  
825 -  
826 - //直接添加 immediUploadDataDic 内容(里面包含 device 信息)  
827 - [collectData addEntriesFromDictionary:self.immediUploadItemDic]; 833 + @synchronized(self.tempEventArray) {
  834 + if ([self.tempEventArray count] > 0) {
  835 + NSMutableDictionary *collectData = [[NSMutableDictionary alloc] initWithContentsOfFile:self.eventFileName];
828 836
829 - //组合event信息  
830 - NSMutableArray *eventJsonDicArray = [NSMutableArray array];  
831 - if ([self.tempEventArray count] > 0) { 837 + //无本地持久化数据
  838 + if (!collectData) {
  839 + collectData = [[NSMutableDictionary alloc]init];
  840 +
  841 + //直接添加 immediUploadDataDic 内容(里面包含 device 信息)
  842 + @synchronized(self.immediUploadItemDic) {
  843 + [collectData addEntriesFromDictionary:self.immediUploadItemDic];
  844 + }
  845 +
  846 + //组合event信息
  847 + NSMutableArray *eventJsonDicArray = [NSMutableArray array];
832 for (YHEvent *aEvent in self.tempEventArray) { 848 for (YHEvent *aEvent in self.tempEventArray) {
833 if (aEvent.jsonDictionary) { 849 if (aEvent.jsonDictionary) {
834 [eventJsonDicArray addObject:aEvent.jsonDictionary]; 850 [eventJsonDicArray addObject:aEvent.jsonDictionary];
835 } 851 }
836 } 852 }
837 853
838 - @synchronized(self.tempEventArray) {  
839 - [self.tempEventArray removeAllObjects];  
840 - }  
841 - }  
842 -  
843 - self.allEventsCount = [eventJsonDicArray count];  
844 - [collectData setObject:eventJsonDicArray forKey:JsonKeyDataTypeEvents];  
845 -  
846 - } else { // 持久化文件不为空  
847 -  
848 - // 检查当前session与本地文件中最近存储的session是否相同  
849 - NSMutableArray *status = [collectData objectForKey:JsonKeyDataTypeStatus];  
850 - if ([status count]) {  
851 - NSMutableDictionary *tempStatus = [status lastObject];  
852 - //如果不同则插入新的状态  
853 - if (![[tempStatus objectForKey:JsonKeyStatusSID] isEqualToString:self.currentStatus.sid]) {  
854 - [status addObject:self.currentStatus.jsonDictionary];  
855 -  
856 - // 更新items中status  
857 - [collectData setObject:status forKey:JsonKeyDataTypeStatus];  
858 - }  
859 - }  
860 -  
861 - //组合event信息  
862 - if ([collectData.allKeys containsObject:JsonKeyDataTypeEvents]) {  
863 - NSMutableArray *oldEvents = [collectData objectForKey:JsonKeyDataTypeEvents];  
864 - if (!oldEvents) {  
865 - oldEvents = [[NSMutableArray alloc]init]; 854 + [self.tempEventArray removeAllObjects];
  855 +
  856 + self.allEventsCount = [eventJsonDicArray count];
  857 + [collectData setObject:eventJsonDicArray forKey:JsonKeyDataTypeEvents];
  858 +
  859 + } else { // 持久化文件不为空
  860 +
  861 + // 检查当前session与本地文件中最近存储的session是否相同
  862 + NSMutableArray *status = [collectData objectForKey:JsonKeyDataTypeStatus];
  863 + if ([status count]) {
  864 + NSMutableDictionary *tempStatus = [status lastObject];
  865 + //如果不同则插入新的状态
  866 + if (![[tempStatus objectForKey:JsonKeyStatusSID] isEqualToString:self.currentStatus.sid]) {
  867 + [status addObject:self.currentStatus.jsonDictionary];
  868 +
  869 + // 更新items中status
  870 + [collectData setObject:status forKey:JsonKeyDataTypeStatus];
  871 + }
866 } 872 }
867 873
868 - if ([self.tempEventArray count] > 0) { 874 + //组合event信息
  875 + if ([collectData.allKeys containsObject:JsonKeyDataTypeEvents]) {
  876 + NSMutableArray *oldEvents = [collectData objectForKey:JsonKeyDataTypeEvents];
  877 + if (!oldEvents) {
  878 + oldEvents = [[NSMutableArray alloc]init];
  879 + }
  880 +
869 for (YHEvent *aEvent in self.tempEventArray) { 881 for (YHEvent *aEvent in self.tempEventArray) {
870 if (aEvent.jsonDictionary) { 882 if (aEvent.jsonDictionary) {
871 [oldEvents addObject:aEvent.jsonDictionary]; 883 [oldEvents addObject:aEvent.jsonDictionary];
872 } 884 }
873 } 885 }
874 - @synchronized(self.tempEventArray) {  
875 - [self.tempEventArray removeAllObjects];  
876 - }  
877 - }  
878 -  
879 - self.allEventsCount = [oldEvents count];  
880 - [collectData setObject:oldEvents forKey:JsonKeyDataTypeEvents];  
881 -  
882 - } else {  
883 - NSMutableArray *newEvents = [[NSMutableArray alloc]init];  
884 -  
885 - if ([self.tempEventArray count] > 0) { 886 + [self.tempEventArray removeAllObjects];
  887 +
  888 + self.allEventsCount = [oldEvents count];
  889 + [collectData setObject:oldEvents forKey:JsonKeyDataTypeEvents];
  890 +
  891 + } else {
  892 + NSMutableArray *newEvents = [[NSMutableArray alloc]init];
  893 +
886 for (YHEvent *aEvent in self.tempEventArray) { 894 for (YHEvent *aEvent in self.tempEventArray) {
887 if (aEvent.jsonDictionary) { 895 if (aEvent.jsonDictionary) {
888 [newEvents addObject:aEvent.jsonDictionary]; 896 [newEvents addObject:aEvent.jsonDictionary];
889 } 897 }
890 } 898 }
891 - @synchronized(self.tempEventArray) {  
892 - [self.tempEventArray removeAllObjects];  
893 - } 899 + [self.tempEventArray removeAllObjects];
  900 +
  901 + self.allEventsCount = [newEvents count];
  902 + [collectData setObject:newEvents forKey:JsonKeyDataTypeEvents];
894 } 903 }
895 -  
896 - self.allEventsCount = [newEvents count];  
897 - [collectData setObject:newEvents forKey:JsonKeyDataTypeEvents];  
898 } 904 }
  905 +
  906 + [collectData writeToFile:self.eventFileName atomically:YES];
899 } 907 }
900 -  
901 - [collectData writeToFile:self.eventFileName atomically:YES];  
902 } 908 }
903 } 909 }
904 910
905 - (void)writeTempPerformanceArray 911 - (void)writeTempPerformanceArray
906 { 912 {
907 - if ([self.tempPerformanceArray count] > 0) {  
908 - NSMutableDictionary *collectData = [[NSMutableDictionary alloc] initWithContentsOfFile:self.eventFileName];  
909 -  
910 - //无本地持久化数据  
911 - if (!collectData) {  
912 - collectData = [[NSMutableDictionary alloc]init]; 913 + @synchronized(self.tempPerformanceArray) {
  914 + if ([self.tempPerformanceArray count] > 0) {
  915 + NSMutableDictionary *collectData = [[NSMutableDictionary alloc] initWithContentsOfFile:self.eventFileName];
913 916
914 - //直接添加 immediUploadDataDic 内容(里面包含 device 信息)  
915 - [collectData addEntriesFromDictionary:self.immediUploadItemDic];  
916 -  
917 - //组合event信息  
918 - NSMutableArray *eventJsonDicArray = [NSMutableArray array];  
919 - if ([self.tempPerformanceArray count] > 0) { 917 + //无本地持久化数据
  918 + if (!collectData) {
  919 + collectData = [[NSMutableDictionary alloc]init];
  920 +
  921 + //直接添加 immediUploadDataDic 内容(里面包含 device 信息)
  922 + @synchronized(self.immediUploadItemDic) {
  923 + [collectData addEntriesFromDictionary:self.immediUploadItemDic];
  924 + }
  925 +
  926 + //组合event信息
  927 + NSMutableArray *eventJsonDicArray = [NSMutableArray array];
920 for (YHPerformance *aEvent in self.tempPerformanceArray) { 928 for (YHPerformance *aEvent in self.tempPerformanceArray) {
921 if (aEvent.jsonDictionary) { 929 if (aEvent.jsonDictionary) {
922 [eventJsonDicArray addObject:aEvent.jsonDictionary]; 930 [eventJsonDicArray addObject:aEvent.jsonDictionary];
923 } 931 }
924 } 932 }
925 - @synchronized(self.tempPerformanceArray){  
926 - [self.tempPerformanceArray removeAllObjects];  
927 - }  
928 - }  
929 -  
930 - // self.allEventsCount = [eventJsonDicArray count];  
931 - [collectData setObject:eventJsonDicArray forKey:JsonKeyDataTypePerfor];  
932 -  
933 - } else { // 持久化文件不为空  
934 -  
935 - // 检查当前session与本地文件中最近存储的session是否相同  
936 - NSMutableArray *status = [collectData objectForKey:JsonKeyDataTypeStatus];  
937 - if ([status count]) {  
938 - NSMutableDictionary *tempStatus = [status lastObject];  
939 - //如果不同则插入新的状态  
940 - if (![[tempStatus objectForKey:JsonKeyStatusSID] isEqualToString:self.currentStatus.sid]) {  
941 - [status addObject:self.currentStatus.jsonDictionary];  
942 -  
943 - // 更新items中status  
944 - [collectData setObject:status forKey:JsonKeyDataTypeStatus];  
945 - }  
946 - }  
947 -  
948 - //组合performance信息  
949 - if ([collectData.allKeys containsObject:JsonKeyDataTypePerfor]) {  
950 - NSMutableArray *oldEvents = [collectData objectForKey:JsonKeyDataTypeEvents];  
951 - if (!oldEvents) {  
952 - oldEvents = [[NSMutableArray alloc]init]; 933 +
  934 + [self.tempPerformanceArray removeAllObjects];
  935 +
  936 + [collectData setObject:eventJsonDicArray forKey:JsonKeyDataTypePerfor];
  937 +
  938 + } else { // 持久化文件不为空
  939 +
  940 + // 检查当前session与本地文件中最近存储的session是否相同
  941 + NSMutableArray *status = [collectData objectForKey:JsonKeyDataTypeStatus];
  942 + if ([status count]) {
  943 + NSMutableDictionary *tempStatus = [status lastObject];
  944 + //如果不同则插入新的状态
  945 + if (![[tempStatus objectForKey:JsonKeyStatusSID] isEqualToString:self.currentStatus.sid]) {
  946 + [status addObject:self.currentStatus.jsonDictionary];
  947 +
  948 + // 更新items中status
  949 + [collectData setObject:status forKey:JsonKeyDataTypeStatus];
  950 + }
953 } 951 }
954 952
955 - if ([self.tempPerformanceArray count] > 0) { 953 + //组合performance信息
  954 + if ([collectData.allKeys containsObject:JsonKeyDataTypePerfor]) {
  955 + NSMutableArray *oldEvents = [collectData objectForKey:JsonKeyDataTypeEvents];
  956 + if (!oldEvents) {
  957 + oldEvents = [[NSMutableArray alloc]init];
  958 + }
  959 +
956 for (YHPerformance *aEvent in self.tempPerformanceArray) { 960 for (YHPerformance *aEvent in self.tempPerformanceArray) {
957 if (aEvent.jsonDictionary) { 961 if (aEvent.jsonDictionary) {
958 [oldEvents addObject:aEvent.jsonDictionary]; 962 [oldEvents addObject:aEvent.jsonDictionary];
959 } 963 }
960 } 964 }
961 - @synchronized(self.tempPerformanceArray){  
962 - [self.tempPerformanceArray removeAllObjects];  
963 - }  
964 - }  
965 -  
966 - // self.allEventsCount = [oldEvents count];  
967 - [collectData setObject:oldEvents forKey:JsonKeyDataTypePerfor];  
968 -  
969 - } else {  
970 - NSMutableArray *newEvents = [[NSMutableArray alloc]init];  
971 -  
972 - if ([self.tempPerformanceArray count] > 0) { 965 + [self.tempPerformanceArray removeAllObjects];
  966 +
  967 + [collectData setObject:oldEvents forKey:JsonKeyDataTypePerfor];
  968 +
  969 + } else {
  970 + NSMutableArray *newEvents = [[NSMutableArray alloc]init];
  971 +
973 for (YHPerformance *aEvent in self.tempPerformanceArray) { 972 for (YHPerformance *aEvent in self.tempPerformanceArray) {
974 if (aEvent.jsonDictionary) { 973 if (aEvent.jsonDictionary) {
975 [newEvents addObject:aEvent.jsonDictionary]; 974 [newEvents addObject:aEvent.jsonDictionary];
976 } 975 }
977 } 976 }
978 - @synchronized(self.tempPerformanceArray){  
979 - [self.tempPerformanceArray removeAllObjects];  
980 - } 977 + [self.tempPerformanceArray removeAllObjects];
  978 +
  979 + self.allEventsCount = [newEvents count];
  980 + [collectData setObject:newEvents forKey:JsonKeyDataTypePerfor];
981 } 981 }
982 -  
983 - self.allEventsCount = [newEvents count];  
984 - [collectData setObject:newEvents forKey:JsonKeyDataTypePerfor];  
985 } 982 }
  983 +
  984 + [collectData writeToFile:self.eventFileName atomically:YES];
986 } 985 }
987 -  
988 - [collectData writeToFile:self.eventFileName atomically:YES];  
989 } 986 }
990 -  
991 } 987 }
992 988
993 #pragma mark - CLLocationManagerDelegate 989 #pragma mark - CLLocationManagerDelegate