Skip to content

Commit

Permalink
Wrap transaction operation retain cycle fix in an experiment (Texture…
Browse files Browse the repository at this point in the history
…Group#1438)

* Wrap transaction operation retain cycle fix in an experiment

This is a follow on to TextureGroup#1429.

* fix typo in experiment name. fix copyright in tests file.
  • Loading branch information
bolsinga authored and hebertialmeida committed May 10, 2019
1 parent ef10f42 commit 026eb1d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 13 deletions.
1 change: 1 addition & 0 deletions Source/ASExperimentalFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ typedef NS_OPTIONS(NSUInteger, ASExperimentalFeatures) {
ASExperimentalTextDrawing = 1 << 12, // exp_text_drawing
ASExperimentalFixRangeController = 1 << 13, // exp_fix_range_controller
ASExperimentalOOMBackgroundDeallocDisable = 1 << 14, // exp_oom_bg_dealloc_disable
ASExperimentalTransactionOperationRetainCycle = 1 << 15, // exp_transaction_operation_retain_cycle
ASExperimentalFeatureAll = 0xFFFFFFFF
};

Expand Down
3 changes: 2 additions & 1 deletion Source/ASExperimentalFeatures.mm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
@"exp_image_downloader_priority",
@"exp_text_drawing",
@"exp_fix_range_controller",
@"exp_oom_bg_dealloc_disable"]));
@"exp_oom_bg_dealloc_disable",
@"exp_transaction_operation_retain_cycle"]));
if (flags == ASExperimentalFeatureAll) {
return allNames;
}
Expand Down
28 changes: 20 additions & 8 deletions Source/Details/Transactions/_ASAsyncTransactionContainer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <AsyncDisplayKit/_ASAsyncTransactionContainer.h>
#import <AsyncDisplayKit/_ASAsyncTransactionContainer+Private.h>

#import <AsyncDisplayKit/ASConfigurationInternal.h>
#import <AsyncDisplayKit/_ASAsyncTransaction.h>
#import <AsyncDisplayKit/_ASAsyncTransactionGroup.h>

Expand Down Expand Up @@ -54,14 +55,25 @@ - (_ASAsyncTransaction *)asyncdisplaykit_asyncTransaction
self.asyncdisplaykit_asyncLayerTransactions = transactions;
}
__weak CALayer *weakSelf = self;
transaction = [[_ASAsyncTransaction alloc] initWithCompletionBlock:^(_ASAsyncTransaction *completedTransaction, BOOL cancelled) {
__strong CALayer *self = weakSelf;
if (self == nil) {
return;
}
[self.asyncdisplaykit_asyncLayerTransactions removeObject:completedTransaction];
[self asyncdisplaykit_asyncTransactionContainerDidCompleteTransaction:completedTransaction];
}];
if (ASActivateExperimentalFeature(ASExperimentalTransactionOperationRetainCycle)) {
transaction = [[_ASAsyncTransaction alloc] initWithCompletionBlock:^(_ASAsyncTransaction *completedTransaction, BOOL cancelled) {
__strong CALayer *self = weakSelf;
if (self == nil) {
return;
}
[self.asyncdisplaykit_asyncLayerTransactions removeObject:completedTransaction];
[self asyncdisplaykit_asyncTransactionContainerDidCompleteTransaction:completedTransaction];
}];
} else {
transaction = [[_ASAsyncTransaction alloc] initWithCompletionBlock:^(_ASAsyncTransaction *completedTransaction, BOOL cancelled) {
__strong CALayer *self = weakSelf;
if (self == nil) {
return;
}
[transactions removeObject:completedTransaction];
[self asyncdisplaykit_asyncTransactionContainerDidCompleteTransaction:completedTransaction];
}];
}
[transactions addObject:transaction];
self.asyncdisplaykit_currentAsyncTransaction = transaction;
[self asyncdisplaykit_asyncTransactionContainerWillBeginTransaction:transaction];
Expand Down
6 changes: 4 additions & 2 deletions Tests/ASConfigurationTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
ASExperimentalImageDownloaderPriority,
ASExperimentalTextDrawing,
ASExperimentalFixRangeController,
ASExperimentalOOMBackgroundDeallocDisable
ASExperimentalOOMBackgroundDeallocDisable,
ASExperimentalTransactionOperationRetainCycle,
};

@interface ASConfigurationTests : ASTestCase <ASConfigurationDelegate>
Expand All @@ -59,7 +60,8 @@ + (NSArray *)names {
@"exp_image_downloader_priority",
@"exp_text_drawing",
@"exp_fix_range_controller",
@"exp_oom_bg_dealloc_disable"
@"exp_oom_bg_dealloc_disable",
@"exp_transaction_operation_retain_cycle"
];
}

Expand Down
37 changes: 35 additions & 2 deletions Tests/ASTransactionTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// ASTransactionTests.m
// AsyncDisplayKitTests
//
// Created by Greg Bolsinga on 3/26/19.
// Copyright © 2019 Pinterest. All rights reserved.
// Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//

#import "ASTestCase.h"
Expand Down Expand Up @@ -52,8 +52,41 @@ - (void)testWeakWhenCancelled
XCTAssertNil(weakTransaction);
}

- (void)testWeakWithSingleOperation_noExperiment
{
__weak _ASAsyncTransaction* weakTransaction = nil;
@autoreleasepool {
CALayer *layer = [[CALayer alloc] init];
_ASAsyncTransaction *transaction = layer.asyncdisplaykit_asyncTransaction;

[transaction addOperationWithBlock:^id<NSObject> _Nullable{
return nil;
} priority:1
queue:dispatch_get_main_queue()
completion:^(id _Nullable value, BOOL canceled) {
;
}];

weakTransaction = transaction;
layer = nil;
}

// held by main transaction group
XCTAssertNotNil(weakTransaction);

// run so that transaction group drains.
static NSTimeInterval delay = 0.1;
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:delay]];

XCTAssertNotNil(weakTransaction); // Oops! Experiment will fix this.
}

- (void)testWeakWithSingleOperation
{
ASConfiguration *config = [[ASConfiguration alloc] initWithDictionary:nil];
config.experimentalFeatures = ASExperimentalTransactionOperationRetainCycle;
[ASConfigurationManager test_resetWithConfiguration:config];

__weak _ASAsyncTransaction* weakTransaction = nil;
@autoreleasepool {
CALayer *layer = [[CALayer alloc] init];
Expand Down

0 comments on commit 026eb1d

Please sign in to comment.