每当我NSLog
在短时间内多次Xcode
时,2015-11-25 15:53:47.207 AppName[97163:38002062] date: 2015-12-28 22:00:00 +0000, day: 29, week: 2015-53
2015-11-25 15:53:47.207 AppName[97163:38002062] date: 2015-12-29 22:00:00 +0002015-11-25 15:53:47.207 AppName[97163:38002062] date: 2015-12-30 22:00:00 +0000, day: 31, week: 2015-53
2015-11-25 15:53:47.208 AppName[97163:38002062] date: 2015-12-31 22:00:00 +0000, day: 1, week: 2015-53
会随机截断其中一些内容,如:
2015-11-25 15:53:47.207 AppName[97163:38002062] date: 2015-12-28 22:00:00 +0000, day: 29, week: 2015-53
2015-11-25 15:53:47.207 AppName[97163:38002062] date: 2015-12-29 22:00:00 +0000, day: 30, week: 2015-53
2015-11-25 15:53:47.207 AppName[97163:38002062] date: 2015-12-30 22:00:00 +0000, day: 31, week: 2015-53
2015-11-25 15:53:47.208 AppName[97163:38002062] date: 2015-12-31 22:00:00 +0000, day: 1, week: 2015-53
它应该是这样的:
for (NSInteger year = 2015; year <= 2016; year++) {
for (NSInteger month = (year == 2015 ? 11 : 1); month <= (year == 2015 ? 12 : 2); month++) {
for (NSInteger day = 1; day <= (month == 11 ? 30 : 31); day++) {
NSDate *date = [NSDate dateWithYear:year month:month day:day];
NSLog(@"INFO: date: %@, day: %d, week: %d-%d", date, (int)day, (int)[date yearForWeekOfYearWithCalendar:calendar], (int)[date weekOfYearWithCalendar:calendar]);
}
}
}
其他时候,日志的顺序也是错误的。
为什么会发生这种情况?如何阻止它?
代码段(需要DateTools):
id value
1 computer
2 electronics
答案 0 :(得分:0)
他们在错误的订单中的事实有时是一个线索 - 看起来你在线程上调用NSLog
,而不仅仅是线程按照你不期望的顺序执行,但显然它们有时会相互踩踏。您可以将对NSLog
的调用包含在dispatch_sync(dispatch_get_main_queue(), ^{ NSLog(...) })
内,以防止它们相互踩踏。但是,如果您希望它们按特定顺序执行,那么调用者必须同步执行,而不是异步执行。