Swift Memory Cycle From ClosureClosure as UIControlEvents handlerremoveAll(closure) in SwiftCounting words...
In Star Trek IV, why did the Bounty go back to a time when whales were already rare?
A known event to a history junkie
Is there an Impartial Brexit Deal comparison site?
Why are on-board computers allowed to change controls without notifying the pilots?
Meta programming: Declare a new struct on the fly
Visiting the UK as unmarried couple
node command while defining a coordinate in TikZ
Giant Toughroad SLR 2 for 200 miles in two days, will it make it?
Indicating multiple different modes of speech (fantasy language or telepathy)
Have I saved too much for retirement so far?
Superhero words!
Is there an wasy way to program in Tikz something like the one in the image?
Science Fiction story where a man invents a machine that can help him watch history unfold
Word describing multiple paths to the same abstract outcome
Simple recursive Sudoku solver
What should I use for Mishna study?
Teaching indefinite integrals that require special-casing
What to do when my ideas aren't chosen, when I strongly disagree with the chosen solution?
Are Warlocks Arcane or Divine?
Resetting two CD4017 counters simultaneously, only one resets
What is the term when two people sing in harmony, but they aren't singing the same notes?
I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?
For airliners, what prevents wing strikes on landing in bad weather?
The most efficient algorithm to find all possible integer pairs which sum to a given integer
Swift Memory Cycle From Closure
Closure as UIControlEvents handlerremoveAll(closure) in SwiftCounting words from standard input in SwiftGenerating Phone Words in SwiftDot product, ported from C to SwiftImplementation of a piece table in SwiftNavigation controller in SwiftiOS Memory Game - Swift 4Closure implementationSwift Throttler and Debouncer
$begingroup$
I am working with closures and want to confirm that I do not have any memory leaks in my code. Below is my current code:
// in ImageGalleryController.swift
class ImageGalleryCollectionViewController: UICollectionViewController {
var images = [Image]()
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
// assignment of closure
var cellImage = Image() { [weak self](image) in
self?.images.append(image)
self?.collectionView.reloadData()
}
// asynchronous
session.loadObjects(ofClass: NSURL.self) { urls in
if let url = urls.first as? URL {
cellImage.url = url
}
}
// asynchronous
session.loadObjects(ofClass: UIImage.self) { images in
if let image = images.first as? UIImage {
cellImage.aspectRatio = Double(image.size.height / image.size.width)
}
}
}
}
// in ImageGallery.swift
struct Image {
var url: URL? = nil {
didSet {
if propertiesAreSet() { handler(self) }
}
}
var aspectRatio: Double? = nil {
didSet {
if propertiesAreSet() { handler(self) }
}
}
init(handler: @escaping (Image) -> Void) {
self.handler = handler
}
var handler: (Image) -> Void
private func propertiesAreSet() -> Bool {
let bool = (url != nil && aspectRatio != nil) ? true : false
return bool
}
}
Basically my logic is that when two separate asynchronous requests get and set two separate properties on the Image struct, then I want to append that Image struct to the images array.
Which brings me to my related questions:
- Since I am fairly new to Swift, am I properly handling memory leaks by declaring [weak self] when I create a new Image instance?
- Should I set the closure handler to nil after it executes?
- Is what I am doing ok or am I violating any Swift principles?
swift
New contributor
$endgroup$
add a comment |
$begingroup$
I am working with closures and want to confirm that I do not have any memory leaks in my code. Below is my current code:
// in ImageGalleryController.swift
class ImageGalleryCollectionViewController: UICollectionViewController {
var images = [Image]()
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
// assignment of closure
var cellImage = Image() { [weak self](image) in
self?.images.append(image)
self?.collectionView.reloadData()
}
// asynchronous
session.loadObjects(ofClass: NSURL.self) { urls in
if let url = urls.first as? URL {
cellImage.url = url
}
}
// asynchronous
session.loadObjects(ofClass: UIImage.self) { images in
if let image = images.first as? UIImage {
cellImage.aspectRatio = Double(image.size.height / image.size.width)
}
}
}
}
// in ImageGallery.swift
struct Image {
var url: URL? = nil {
didSet {
if propertiesAreSet() { handler(self) }
}
}
var aspectRatio: Double? = nil {
didSet {
if propertiesAreSet() { handler(self) }
}
}
init(handler: @escaping (Image) -> Void) {
self.handler = handler
}
var handler: (Image) -> Void
private func propertiesAreSet() -> Bool {
let bool = (url != nil && aspectRatio != nil) ? true : false
return bool
}
}
Basically my logic is that when two separate asynchronous requests get and set two separate properties on the Image struct, then I want to append that Image struct to the images array.
Which brings me to my related questions:
- Since I am fairly new to Swift, am I properly handling memory leaks by declaring [weak self] when I create a new Image instance?
- Should I set the closure handler to nil after it executes?
- Is what I am doing ok or am I violating any Swift principles?
swift
New contributor
$endgroup$
add a comment |
$begingroup$
I am working with closures and want to confirm that I do not have any memory leaks in my code. Below is my current code:
// in ImageGalleryController.swift
class ImageGalleryCollectionViewController: UICollectionViewController {
var images = [Image]()
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
// assignment of closure
var cellImage = Image() { [weak self](image) in
self?.images.append(image)
self?.collectionView.reloadData()
}
// asynchronous
session.loadObjects(ofClass: NSURL.self) { urls in
if let url = urls.first as? URL {
cellImage.url = url
}
}
// asynchronous
session.loadObjects(ofClass: UIImage.self) { images in
if let image = images.first as? UIImage {
cellImage.aspectRatio = Double(image.size.height / image.size.width)
}
}
}
}
// in ImageGallery.swift
struct Image {
var url: URL? = nil {
didSet {
if propertiesAreSet() { handler(self) }
}
}
var aspectRatio: Double? = nil {
didSet {
if propertiesAreSet() { handler(self) }
}
}
init(handler: @escaping (Image) -> Void) {
self.handler = handler
}
var handler: (Image) -> Void
private func propertiesAreSet() -> Bool {
let bool = (url != nil && aspectRatio != nil) ? true : false
return bool
}
}
Basically my logic is that when two separate asynchronous requests get and set two separate properties on the Image struct, then I want to append that Image struct to the images array.
Which brings me to my related questions:
- Since I am fairly new to Swift, am I properly handling memory leaks by declaring [weak self] when I create a new Image instance?
- Should I set the closure handler to nil after it executes?
- Is what I am doing ok or am I violating any Swift principles?
swift
New contributor
$endgroup$
I am working with closures and want to confirm that I do not have any memory leaks in my code. Below is my current code:
// in ImageGalleryController.swift
class ImageGalleryCollectionViewController: UICollectionViewController {
var images = [Image]()
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
// assignment of closure
var cellImage = Image() { [weak self](image) in
self?.images.append(image)
self?.collectionView.reloadData()
}
// asynchronous
session.loadObjects(ofClass: NSURL.self) { urls in
if let url = urls.first as? URL {
cellImage.url = url
}
}
// asynchronous
session.loadObjects(ofClass: UIImage.self) { images in
if let image = images.first as? UIImage {
cellImage.aspectRatio = Double(image.size.height / image.size.width)
}
}
}
}
// in ImageGallery.swift
struct Image {
var url: URL? = nil {
didSet {
if propertiesAreSet() { handler(self) }
}
}
var aspectRatio: Double? = nil {
didSet {
if propertiesAreSet() { handler(self) }
}
}
init(handler: @escaping (Image) -> Void) {
self.handler = handler
}
var handler: (Image) -> Void
private func propertiesAreSet() -> Bool {
let bool = (url != nil && aspectRatio != nil) ? true : false
return bool
}
}
Basically my logic is that when two separate asynchronous requests get and set two separate properties on the Image struct, then I want to append that Image struct to the images array.
Which brings me to my related questions:
- Since I am fairly new to Swift, am I properly handling memory leaks by declaring [weak self] when I create a new Image instance?
- Should I set the closure handler to nil after it executes?
- Is what I am doing ok or am I violating any Swift principles?
swift
swift
New contributor
New contributor
New contributor
asked yesterday
DarkisaDarkisa
1011
1011
New contributor
New contributor
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Darkisa is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216120%2fswift-memory-cycle-from-closure%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Darkisa is a new contributor. Be nice, and check out our Code of Conduct.
Darkisa is a new contributor. Be nice, and check out our Code of Conduct.
Darkisa is a new contributor. Be nice, and check out our Code of Conduct.
Darkisa is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216120%2fswift-memory-cycle-from-closure%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown