Skip to content

Instantly share code, notes, and snippets.

View felipeslongo's full-sized avatar
🌴
Having Fun With Threads

Felipe de Souza Longo felipeslongo

🌴
Having Fun With Threads
View GitHub Profile
@felipeslongo
felipeslongo / UITableViewCell+GetTableView.cs
Created March 22, 2019 21:24
Xamarin.IOS extension that gets the parent UITableView from the UITableViewCell
namespace UIKit
{
/// <summary>
/// Xamarin.IOS extension that gets the parent <see cref="UITableView"/> from the <see cref="UITableViewCell"/>
/// </summary>
/// <seealso cref="https://stackoverflow.com/questions/15711645/how-to-get-uitableview-from-uitableviewcell"/>
public static class UITableViewCell_GetTableView
{
/// <summary>
/// Gets the parent <see cref="UITableView"/>
@felipeslongo
felipeslongo / UITableView+SelectCell.cs
Created March 22, 2019 17:01
Xamarin.IOS extension method that performs a click/tap event into a UITableViewCell
namespace UIKit
{
public static class UITableView_SelectCell
{
public static void SelectCell(this UITableView @this, UITableViewCell cell)
{
var index = @this.IndexPathForCell(cell);
@this.SelectRow(index, true, UITableViewScrollPosition.None);
@this.Delegate.RowSelected(@this, index);
}
@felipeslongo
felipeslongo / UITableView_ScrollToAndSelectRow.cs
Created March 21, 2019 22:58
Xamarin.IOS extension that scroll to a UITableViewCell and select it (it trigger the selected events)
using System;
using System.Threading.Tasks;
using Foundation;
namespace UIKit
{
public static class UITableView_ScrollToAndSelectRow
{
private static TimeSpan CabalisticDelay => TimeSpan.FromSeconds(1);
@felipeslongo
felipeslongo / .View
Created March 18, 2019 21:31 — forked from aspnetde/.View
Xamarin.iOS MemoryUtility
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.MessageUI;
using MonoTouch.UIKit;
namespace MyApp
{
public interface ICanCleanUpMyself
{
@felipeslongo
felipeslongo / DisposeEx.cs
Last active March 18, 2019 20:55
I used the below extension methods to solve these memory leak issues. Think of Ender's Game final battle scene, the DisposeEx method is like that laser and it disassociates all views and their connected objects and disposes them recursively and in a way that shouldn't crash your app.
//https://stackoverflow.com/questions/25532870/xamarin-ios-memory-leaks-everywhere?rq=1
public static void DisposeEx(this UIView view) {
const bool enableLogging = false;
try {
if (view.IsDisposedOrNull())
return;
var viewDescription = string.Empty;
@felipeslongo
felipeslongo / CultureInfo_Setters.cs
Last active March 13, 2019 17:29
Set CultureInfo for the entire application
using System.Threading;
namespace System.Globalization
{
public static class CultureInfo_Setters
{
public static void SetForApplication(this CultureInfo @this)
{
@this.SetForCurrentThread();
@this.SetForThreadPoolThreads();
@felipeslongo
felipeslongo / Task_FireAndForget.cs
Created March 6, 2019 20:27
Task FireAndForget Extension Method
static async void FireAndForget(this Task task)
{
try
{
await task;
}
catch (Exception e)
{
// log errors
}
static void async Main(string[] args)
{
try
{
await ThrowExceptionAfterAsync();
}
catch(Exception e)
{
//Gonna catch SHU
Console.WrileLine(e.Message);//SHU
@felipeslongo
felipeslongo / AsyncAwaitFireAndForget.cs
Last active February 27, 2019 14:26
Fire and Forge: How it changes the way the caller works
static void async Main(string[] args)
{
await FireAndForget();
Console.WriteLine("Caller");
FireAndForget();
Console.WriteLine("Caller");
}
public static async Task FireAndForget()
static void Main(string[] args)
{
var result = await SumAsync(1,2);
Console.WriteLine(result);
}
public static int Sum(int a, int b) => a + b;
public static Task<int> SumAsync(int a, int b) => Task.Run(() => Sum(a, b));