pEpObjCAdapter/PEPSessionProvider.m
branchIOSAD-50
changeset 290 19e6db3d1767
parent 288 a96780f56daa
child 292 ee62eebf395b
     1.1 --- a/pEpObjCAdapter/PEPSessionProvider.m	Fri Oct 20 00:22:44 2017 +0200
     1.2 +++ b/pEpObjCAdapter/PEPSessionProvider.m	Fri Oct 20 12:47:55 2017 +0200
     1.3 @@ -23,12 +23,25 @@
     1.4  static NSLock *s_sessionForThreadLock = nil;
     1.5  static NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *s_sessionForThreadDict;
     1.6  
     1.7 +/** We have to conform to the Engines rule: "The first session has to be created on the main thread and kept
     1.8 + alive until all sessiopns created afterwards have been teared down."
     1.9 + Here we hold it.
    1.10 + */
    1.11 +static PEPInternalSession *s_sessionForMainThread = nil;
    1.12 +
    1.13  #pragma mark - Public API
    1.14  
    1.15  + (PEPInternalSession * _Nonnull)session
    1.16  {
    1.17      [[self sessionForThreadLock] lock];
    1.18  
    1.19 +    // Assure a session for the main thread exists and is kept alive before anyother session is created.
    1.20 +    [self assureSessionForMainThreadExists];
    1.21 +
    1.22 +    if ([NSThread isMainThread]) {
    1.23 +        return s_sessionForMainThread;
    1.24 +    }
    1.25 +
    1.26      NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *dict = [self sessionForThreadDict];
    1.27      PEPCopyableThread *currentThread = [[PEPCopyableThread alloc] initWithThread:[NSThread currentThread]];
    1.28      PEPInternalSession *session = dict[currentThread];
    1.29 @@ -39,11 +52,12 @@
    1.30      [self nullifySessionsOfFinishedThreads];
    1.31  
    1.32  //    NSLog(@"#################\nnum sessions is now %lu",
    1.33 -//          (unsigned long)[self sessionForThreadDict].count);
    1.34 +//          (unsigned long)[self sessionForThreadDict].count + (s_sessionForMainThread ? 1 : 0));
    1.35  //    NSLog(@"Threads:");
    1.36  //    for (PEPCopyableThread *thread in dict.allKeys) {
    1.37  //        NSLog(@"%@", thread.description);
    1.38  //    }
    1.39 +//    NSLog(@"Session for main thread: %@", s_sessionForMainThread);
    1.40  //    NSLog(@"##################################");
    1.41  
    1.42      [[self sessionForThreadLock] unlock];
    1.43 @@ -81,6 +95,23 @@
    1.44  }
    1.45  
    1.46  #pragma mark -
    1.47 +/**
    1.48 + Assures a session for the main thread is set.
    1.49 + */
    1.50 ++ (void)assureSessionForMainThreadExists
    1.51 +{
    1.52 +    if (s_sessionForMainThread) {
    1.53 +        return;
    1.54 +    }
    1.55 +
    1.56 +    if ([NSThread isMainThread]) {
    1.57 +        s_sessionForMainThread = [[PEPInternalSession alloc] initInternal];
    1.58 +    } else {
    1.59 +        dispatch_sync(dispatch_get_main_queue(), ^{
    1.60 +            s_sessionForMainThread = [[PEPInternalSession alloc] initInternal];
    1.61 +        });
    1.62 +    }
    1.63 +}
    1.64  
    1.65  + (void)cleanupInternal
    1.66  {
    1.67 @@ -89,8 +120,10 @@
    1.68      for (PEPCopyableThread *thread in dict.allKeys) {
    1.69          [self nullifySessionForThread:thread];
    1.70      }
    1.71 +    s_sessionForMainThread = nil;
    1.72      [dict removeAllObjects];
    1.73 -//    NSLog(@"All sessions have been cleaned up. Session count is %lu", (unsigned long)dict.count);
    1.74 +//    NSLog(@"All sessions have been cleaned up. Session count is %lu",
    1.75 +//          (unsigned long)dict.count + (s_sessionForMainThread ? 1 : 0));
    1.76  }
    1.77  
    1.78  /**