Skip to content

Instantly share code, notes, and snippets.

-----BEGIN PGP PUBLIC KEY BLOCK-----
mQGNBGC1kmYBDAC7yME2vrTohDoWnaf/YQOMW0REDmWuDHNz+IsWixmnzertmqgC
xO53TOmlUrOnAAm6SL/fM+SGgjq5wC6fNZq3mfRP/6uZPRBSi3ispAa1pRY/9ams
gSLwZjEA9gRnZlrYtqbQKx2VAPy72d8oybK0+zZ3Kqi2X6VCk7BjB6JhJ/IcQeAE
imIzkkjtFpHAiUPGZ7apfqwLeFqbUoHPW2WBP7h//cF75mRQHfSDIC5a8Yts2dml
9BHk/tWtNJpSiQS2JchUui+hXT2gLD5Sw1+WYGLsRiknHRhEv6xBV6Fr8vDfqWcx
FvMlkZsDsmoqKNdae/GjmJ7X0Ytj6LS/9cgTNfyISvAZNiQ9nQq2YALXnjf/okeO
RlKvMHaO+J21I8wqb/1fpnuQ879yyug/kYrbVDoZlWia3RWAqKm5alEV3hEpTnV7
gGCQQE4ILQjKcBHPmySwvsCIVJTzlhtzfwujiQucXIAUTiFsbjRJ+ZwqD2bukcLE
/*
Here's our goal:
let localDataStore = UserDataStore()
let remoteDataStore = UserApi()
let dataStore = CacheBackedDataStore(localDataStore, remoteDataStore)
dataStore.fetch(userID) { result in
// handle result
}
@rdv0011
rdv0011 / swift-combine-retry.md
Last active September 5, 2022 09:25
Retry operation in Swift/Combine.

There is a function .retry() in Combine that helps to retry a request when something goes wrong with a long runing task. Although it is not enough just to call retry() to achieve retrying logic. The retry() function does not do another request but it re-subscribes only to a publisher. The below approaches might be used to solve the problem.

Using tryCatch

To make another request the tryCatch() might be used. In the code below if the first call fails there are three attempts to retry (retry(3)) that are made:

import UIKit
import Combine
import PlaygroundSupport

enum CustomNetworkingError: Error {
    case invalidServerResponse
@mycodeschool
mycodeschool / DoublyLinkedList.c
Created November 12, 2013 11:38
Doubly Linked List implementation in C
/* Doubly Linked List implementation */
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};